Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve an Image stored as BLOB on a MYSQL DB

I'm trying to create a PDF based on the information that resides on a database. Know I need to retrieve a TIFF image that is stored as a BLOB on a mysql database from Java. And I don't know how to do it. The examples I've found shows how to retrieve it and save it as a File (but on disk) and I needed to reside on memory.

Table name: IMAGENES_REGISTROS

BLOB Field name: IMAGEN

Any Ideas?

like image 987
Sheldon Avatar asked Jan 27 '10 21:01

Sheldon


People also ask

How do I recover my BLOB image?

The getBlob() method of PreparedStatement is used to get Binary information, it returns the instance of Blob. After calling the getBytes() method on the blob object, we can get the array of binary information that can be written into the image file.


1 Answers

On your ResultSet call:

Blob imageBlob = resultSet.getBlob(yourBlobColumnIndex);
InputStream binaryStream = imageBlob.getBinaryStream(0, imageBlob.length());

Alternatively, you can call:

byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());

As BalusC noted in his comment, you'd better use:

InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);

And then the code depends on how you are going to read and embed the image.

like image 172
Bozho Avatar answered Oct 05 '22 06:10

Bozho