Servlet回复getHttpConnection

时间:2015-05-21 12:37:10

标签: java android http servlets get

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

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

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();
}

我希望收到包含InputStream图片的Bitmap,但收到以下错误:

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 个答案:

答案 0 :(得分: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();
}