Retrieve an Image stored as BLOB on a MYSQL DB

前端 未结 4 721
挽巷
挽巷 2020-11-27 16:37

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.

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 17:13

    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.

提交回复
热议问题