views:

615

answers:

1

Hi folks

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?

+3  A: 

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.

Bozho
Thanks, just a comment. imageBlob returns a Long. And getBytes expect an Integer. I parse (Integer.ParseInt..) the Long, and it works. I don't know if eventually that'll gonna bring problems.Is there any other way? thanks
Sheldon
Don't use `ResultSet#getBlob()` or `ResultSet#getBytes()`. Just use `ResultSet#getBinaryStream()`.
BalusC
is it just a convenient method, or there is something more to it?
Bozho