Convert BufferedInputStream into an Image
You encounter an issue converting a blob from a database, which you assume to be an image in JPEG format, into a BufferedImage for further processing. The conversion fails, and your Image variable remains null.
Possible Reasons for Conversion Failure
Upon examining your code, several potential problems can cause the conversion to fail:
Solution
To resolve the issues, try modifying your code as follows:
public Response post(@PathParam("id") String id) throws IOException { Connection con = connection(); Blob blob = getPhoto(con); BufferedImage image = null; InputStream blobStream = null; int blobLength = 0; try { blobLength = (int) blob.length(); blobStream = blob.getBinaryStream(1, blobLength); image = ImageIO.read(blobStream); } catch (SQLException e2) { e2.printStackTrace(); } return Response.ok(image).build(); }
Additionally, you should verify the validity of the uploadedInputStream by writing it to a file and reading it back in to ensure it contains an image.
The above is the detailed content of Why is my `BufferedImage` null when converting a Blob from a database?. For more information, please follow other related articles on the PHP Chinese website!