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


Reply With Quote