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?
You are really not clear about what is actually happening, which is perhaps just ignorance. But storing and passing an
InputStreamaround in the session is already not a good sign. Firstly, it is not serializable. Secondly, you’re fully detaching the input stream from the context where it’s been created (so it might implicitly have been closed/released when the initial context is finished). Thirdly, an input stream can often be read only once (so once it’s read, it cannot be read again anymore, you’d have to create a new one).The normal approach is to read the
InputStreaminto abyte[]directly after its creation and then store thatbyte[]in the session instead.And then in the image servlet, just do
You only need to be aware that each byte of a
byte[]eats one byte of JVM’s memory. Be sure that you don’t go overboard. Remove the attribute from the session as soon as you don’t need it anymore. Make use of temp file storage if necessary, for sure if you have to deal with large files.Update: as per your comment on the question:
A content length of 0 confirms that the input stream was already been read (or its source has implicitly been released). This only confirms my initial guesses. No, manually setting the content length header won’t solve the problem. The servlet container already automatically takes care about it when the response body fits fully in the default response buffer; it would otherwise switch to chunked encoding anyway.