Cris
Cris

Reputation: 2021

Servlet reply to getHttpConnection

I have some images in my Servlet that I want to download into my Android app.

I am performing a GET request to this URL:

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

And this class doing the job:

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

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

I would like to receive an InputStream containing my Bitmap image but I receive the following error:

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)
...

Upvotes: 2

Views: 217

Answers (1)

Tarlog
Tarlog

Reputation: 10154

Why do you need to use ImageIO? You can do simply something like:

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

Upvotes: 1

Related Questions