Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I manage to save my BufferedImage into mysql db as BLOP with the help of ByteArrayOutputStream.

But now i want to retrieve and convert back to the original BufferedImage, how do i do that? Below is what i tried but the result (BufferedImage) is wrong.

What I have tried:

Uploading to database:
C#
 ByteArrayOutputStream outClean = new ByteArrayOutputStream();
ImageIO.write(imgClean, "png", outClean);
byte[] bufClean = outClean.toByteArray();
// setup stream for blob
ByteArrayInputStream streamClean = new ByteArrayInputStream(bufClean);

String sql = "INSERT INTO users (userName, userCleanImage)"
        + "VALUES (?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, username);
statement.setBinaryStream(2, streamClean, streamClean.available());


int n = statement.executeUpdate();


Retrieve from database
C#
    ResultSet rs = null;
BufferedImage bufImg = null;
try {
    String sql = "Select userCleanImage from users"
                    + "where userName = ?";
    PreparedStatement statement = conn.prepareStatement(sql);
    statement.setString(1, username);
    rs = statement.executeQuery();
    Blob aBlob = rs.getBlob("userCleanImage");
    InputStream is = aBlob.getBinaryStream(0, aBlob.length());
    bufImg = ImageIO.read(is);

return bufImg;
} catch (SQLException e) {

}catch (IOException io) {

}
Posted
Updated 19-May-20 6:28am

1 solution

I just called aBlob.getBinaryStream() with no parameters and it works for me. The function with pos and length parameters throws an exception java.lang.AbstractMethodError: oracle.sql.BLOB.getBinaryStream(JJ)Ljava/io/InputStream; which I was reading can mean that your version of ojdbc does not support that method call. The documentation says it's for returning a partial blob. I don't know why you would want a partial doesn't really sound too useful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900