有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Servlet对getHttpConnection的回复

我的Servlet中有一些图像,我想下载到我的Android应用程序中

我正在对此URL执行GET请求:

public static final String URL ="http://myIpAddress:8080/imgs";

这班学生正在做这项工作:

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
  StaggeredPrenotaTour staggeredPrenotaView;

    public GetXMLTask(StaggeredPrenotaTour listView){
        this.staggeredPrenotaView = listView;
    }

    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        staggeredPrenotaView.pullTours(result); 
    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

在我的Servlet中:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    InputStream st = getServletContext().getResourceAsStream("/WEB-INF/imgs/cardbackground9.jpg");

    BufferedImage bi = ImageIO.read(st); **//error here**

    OutputStream out = resp.getOutputStream();

    //Todo send InputStream into OutputStream
    ImageIO.write(bi, "jpg", out);

    out.close();
}

我想接收包含我的Bitmap图像的InputStream,但我收到以下错误:

WARNING: Error for /imgs
java.lang.NoClassDefFoundError: javax.imageio.ImageIO is a restricted class.Please see the Google  App Engine developer's guide for more details.
at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
at madapps.bicitourbo.backend.MyServlet.doGet(MyServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
...

共 (1) 个答案

  1. # 1 楼答案

    为什么需要使用ImageIO?您可以简单地执行以下操作:

    InputStream st = getServletContext().getResourceAsStream("/WEB-INF/imgs/cardbackground9.jpg");
    
    OutputStream os = resp.getOutputStream();
    
    if (st != null) {
           byte[] buf = new byte[4096];
           int nRead;
           while( (nRead=st.read(buf)) != -1 ) {
                 os.write(buf, 0, nRead);
           }
           st.close();
    }