2

I am trying to display an image on jsp from local drive. I set the img src as src = "../../../ImageServlet" in jsp but I am getting a

java.lang.IllegalStateException 
    at org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(ServletResponseWrapperInclude.java:63) 
    at DisplayImageServlet.doPost(ImageServlet.java:95)
    at DisplayImageServlet.doGet(ImageServlet.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

exception.

My Servlet Code is:

protected void doPost(HttpServletRequest request,HttpServletResponse response)    throws   ServletException, IOException {
String applicationNumber = (String)session.getAttribute("APPLICATIONNUMBER");
File file = new File("C:/photos/"+applicationNumber+".jpg");

        response.setContentType(getServletContext().getMimeType(file.getName()));
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            input = new BufferedInputStream(new FileInputStream(file));
            output = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[10240];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
            if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
        }
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}

and my jsp img tag is like-

<div>
                <div>
                  <label>Photo </label>
                  <div>
                    <div style="width:194px;height:162px;"><img src="../../../ImageServlet" id="Image" style="width:100px;height:100px;" /></div>
                    <div style="margin-left:115px; margin-top:10px;">
                      <input  name="filename" type="file" id="filename"/>
                    </div>
                  </div>
                </div>

Please help me out or is there any other way to do the same. Thanks.

5
  • 2
    I think response.getOutputStream() throws the exception, rather than new BufferedOutputStream(...). Probably the response isn't writable yet. Commented Dec 24, 2013 at 14:33
  • I have already checked the response and it is not committed. response.isCommitted() is returning as false. Commented Dec 24, 2013 at 14:40
  • 3
    Do you have any filters configured? If so, do these use response.getWriter() ? You can only do EITHER response.getWriter() OR response.getOutputStream(). Commented Dec 24, 2013 at 15:13
  • It is not allowed to call that in INCLUDE dispatch. Not sure why they have this limitation... github.com/apache/tomcat/blob/trunk/java/org/apache/jasper/… Commented Dec 24, 2013 at 16:49
  • Thanks everyone for your reply... Commented Dec 26, 2013 at 11:43

1 Answer 1

2

@user3035077 try the following code..

try {
      HttpSession session=request.getSession();
      String applicationNumber = (String)session.getAttribute("APPREFNUMBER");
      File file = new File("C:/photos/"+applicationNumber+".jpg");
      if (file.exists()) {
         BufferedInputStream in = new BufferedInputStream(
                                new FileInputStream(file));

         // Get image contents.
        byte[] bytes = new byte[in.available()];
        in.read(bytes);
        in.close();
         // Write image contents to response.
        response.getOutputStream().write(bytes);
          }
} catch (IOException e) {
        e.printStackTrace();
    }

Let me know if this helps...

Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome, Mark this as answer, so others will get benefit from this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.