I am storing some images in a MySQL database using
ps.setBinaryStream(1, photo);
where photo is an InputStream.
When I read the image from database, I need to convert it to a byte[]. How can I do this?
You could try something like:
File file=new File("E:\\image1.png");
FileOutputStream fos=new FileOutputStream(file);
byte b[];
Blob blob;
PreparedStatement ps=con.prepareStatement("select * from image_table");
ResultSet rs=ps.executeQuery();
while(rs.next()){
blob=rs.getBlob("image");
b=blob.getBytes(1,(int)blob.length());
fos.write(b);
}
The maximum length of Blob type is 64kb.Use bigger types like mediumblob or longblob.
You can also retrieve by using :
inputStream imgStream = resultSet.getBinaryStream(1);
as you stored the image using InputStream
FileOutputStream at least with some JVMs out there.
ResultSet.getBytes(..)?