Announcement Announcement Module
Collapse
No announcement yet.
Servlet writing byte[] image in jsp page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Servlet writing byte[] image in jsp page

    I would be very greatful if someone could take time to comment....

    I have been playing with the petclinic example that comes with the springframwork distribution and have added some functionality.

    I have a webflow where Im suppose to upload an image on page one and show the image and text on page 2 and confirm and save on DB on page 3. Its no problem to upload and save the image (byte[]) in the mysql database but the problem is to show the image on page 2.

    In my jsp-page where Im suppose to show the image Im calling a servlet that is suppose to print out the (byte[]) that attached in the request brings from page 1 but I get an illegalStateException when I call the "ServletOutputStream out = res.getOutputStream();" method..... ?

    I have studied the imagedb example without seeing any usable content to apply on my application. I have also tryed many different angles and examples and searched the web.

    the jsp page 1 that will upload the image.
    The class ad holds the byte[] attribute "theByteImage" which have getters and setters. Im only showing the main tags from jsp-page 1 here.

    <form method="post" enctype="multipart/form-data">
    <spring:bind path="ad.theByteImage">
    <FONT color="red">
    <B><c:out value="${status.errorMessage}"/></B>
    </FONT>
    <BR><input type="file" name="theByteImage"/>
    </spring:bind>
    </form>

    The jsp page that is suppose to show the image looks like this:
    <form method="post">
    <img src="/Image"/>
    </form>

    The Image servlet looks like this:
    public class Image extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    Ad ad = (Ad)req.getAttribute("ad");
    try{
    if (FileUpload.isMultipartContent(req)){
    byte[] image = ad.getTheByteImage();
    if (image.length > 0){
    res.setContentType("image/jpeg");
    res.setStatus(HttpServletResponse.SC_OK);
    ServletOutputStream out = res.getOutputStream();
    out.write(image);
    out.flush();
    out.close();
    }// if
    }// if
    }catch(Exception e){
    System.out.println("Exception------>"+e);
    }
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    System.out.println("=========servlet dopost....");
    doGet(req,res);
    }
    }

    My web.xml looks lite this:
    <servlet>
    <servlet-name>Image</servlet-name>
    <servlet-class>org.springframework.samples.petclinic.util.I mage</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Image</servlet-name>
    <url-pattern>/Image</url-pattern>
    </servlet-mapping>

    My applicationContext-hibernate holds the multipartResolver:
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
    <property name="maxUploadSize">
    <value>100000</value>
    </property>
    </bean>

    The main problem is that I get an java.lang.illegalStateException in the servlet when it comes to the "ServletOutputStream out = res.getOutputStream();" code but if you have suggestions about the code and if I missed something. Im way into my second week trying to solve this problem and I see many other have the same postings eaven though I haven't seen any solution printing out an byte[] in the servlet....

    thanks

  • #2
    How is the Image servlet being referenced? While the JSP/HTML looks ok to me, you will either need to store the Ad as a session attribute or pass it as a parameter and look it up (unless you are using a Filter). While I can't see enough to be sure, it seems to me that the most probable cause is that you are attempting to use one request/response cycle to both upload the image and display it - if this is what you are doing you simply need to upload the image to one Controller which returns a page view, and then reference the Image servlet path as you are - at this point the request will not contain Multipart content. If this is not what is going on please post some of your stack trace and any other details you can think of.

    Comment


    • #3
      Hi guys

      I have similar problem here. Does anyone knows some good tutorial about displaying image, derived from data base, on jsp page ?

      Comment

      Working...
      X