Ace
Ace

Reputation: 7

Converting a Blob to an image

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

Upvotes: 1

Views: 4153

Answers (2)

BalusC
BalusC

Reputation: 1109745

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.

Upvotes: 1

bakoyaro
bakoyaro

Reputation: 2558

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

Upvotes: 0

Related Questions