1

I have stored an image in a database using Blob type. Now I can fetch this image from database using the select. I want to display this image on my jsp. In Jsp i have created a table and want it to be one of the column.

I have blob data how can I convert it back to an image and display it on my jsp page.

Thanks

Ace
  • 7
  • 1
  • 4
  • possible duplicate of [How to retrieve and display images from a database in a JSP page?](http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page) – KNU Apr 01 '14 at 19:02

2 Answers2

1

Just obtain an InputStream of the blob using ResultSet#getBinaryStream() and write it immediately to the OutputStream of the HTTP response along a correct set of headers. There's absolutely no need to store it temporarily on disk as suggested in other answer.

More detail and complete code example can be found in this answer.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Here are two options:

Option 1:

This may not be the best answer, but it will do in a pinch...

You would need to use a InputStreamReader to read the blob out of the column, and then create a FileOutputStream and then create the file in a 'created' images directory. For example if you store you image files in a directory called /pics, you could write the files out to a directory called /pics/generated.

There are some drawbacks to this approach. For one you would generate a file every time the page is requested. which may work if the files will all have different names.

Option 2:

You would use an InputStreamReader to read the blob from the row that is returned from your SQL. Create an OutputStream, using a byte[] as the stream destination. Then it's just a matter of writing the byte[] out to your JSP

bakoyaro
  • 2,550
  • 3
  • 36
  • 63
  • Thanks after 4 hours I am finally done. The problem was not in creating the File from byte[] but was wit displaying it on screen. Coz if I save it on my C:\ i am nt able to use it so had too get context path and all i.e. used this "this.getServletContext().getRealPath("/Soccer")" and this stored the generated image in the web folder. Now I could access these generated pic.... phew..... – Ace Nov 24 '10 at 08:05
  • @Ace Great job, I'll keep that in mind the next time I have to do the same. – bakoyaro Nov 24 '10 at 18:07