I have a servlet which is used to display image.This servlet actually called by the
<img src="/displaySessionImage?widgetName=something"/>
My get & post redirect to this method,
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String widgetName = request.getParameter("widgetName");
try {
//this is my file manager which was store ealier
StorageFile file = (StorageFile)session.getAttribute(widgetName);
response.setContentType(file.getContentType());
//the file manager can retrieve input stream
InputStream in = file.getInputStream();
OutputStream outImage = response.getOutputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
outImage.write(buf, 0, count);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But this code does not work, the image could not be display. I think this will not work because i have store the file manager that contain the input stream in a session. This same method work for another image file that was retrieved from database and not stored in the session. i have actually print out the input stream. it contain the same input stream as the database file.
Is it something wrong with the code? or i actually cannot store the file manager that contain the input stream in a session? or is it that i used input stream in a wrong way?