Alone as an Island What’s the pride in being a Programmer?
May 11

I happen to face this problem a year back.  I was expected to show an image on a JSP which is stored as a BLOB in the database.  (I also wrote another post a few days back with uploading and showing image from a MySQL database (with Hibernate)).  The problem I faced a year back was that i was displaying junk values on the page instead of the image though the MIME type was set as “image/jpeg” and  the output stream flushed.

The same kind of problem my friend faced last week when he was trying to extract information from the database and export it as excel sheet.  He used Apache POI for constructing the workbook, set the MIME type as “application/vnd.ms-excel” and flushed the output stream. But what got exported was just junk values.

The reason was that only the ServletOutputStream was flushed out. Not the response. As you already know that in Servlets, the response does not get flushed automatically (unlike JSPs). So, here comes the need for response.flushBuffer();

Here is the core code.

response.reset();

response.setContentType(“application/vnd.ms-excel”);response.addHeader(“Content-Disposition”,“filename=TableList.xls”);

 

ServletOutputStream stream=response.getOutputStream();

wb.write(stream);

 

//flush output stream

stream.flush();

 

//please dont forget to flush the response

response.flushBuffer();

 

//need to close the stream now. .

 

stream.close();

I developed a small sample application. Please dont mind the architecture or performance of the code. Download the entire source code.

Please note that I have not defined any welcome-files. Point your browser to export.jsp. (PS :Application works only against Oracle databases) 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , , , , ,

No Responses to “Junk values during Excel export/Image display”

  1. Sasisekar Shanmugasundaram Says:

    I am tried using POI and also JXL, And I tried all the tricks with the response headers, server MIME settings, and request string, etc … to get a Excel streamed through the response.
    I keep on getting the junk values.

    After seeing your solution, I figured that the only thing which i did not try is response.flushBuffer();

    Even that ain’t working. Have not figured out a work around to avoid the junk values.

    I know this is a old post, But …. if you get to see my comment … post me a message.

Leave a Reply

Give your best to the world.