0

I converted an image file to byte array and then save as a BLOB in database but when i retrieve the saved byte array from database and converted it back to the image file, the image file is not showing.

converted image file to byte array like this:

File imageFile = new File("image.jpg");
byte[] byteArrayOfImageFile = new byte[(int)imageFile.length()];
FileInputStream fileInputStream = new FileInputStream(imageFile);
fileInputStream.read(byteArrayOfImageFile);
fileInputStream.close();

converted byte array to image file like this:

FileOutputStream fos = new FileOutputStream("image.jpg);
try {
    fos.write(byteArrayOfImageFile);
} finally {
    fos.close();
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
ad3bay0
  • 123
  • 1
  • 9
  • 1
    Here is an answer for this question : [Byte Array to Image File](https://stackoverflow.com/a/1580285) –  Oct 12 '17 at 19:07
  • 2
    You forgot to check the return value of the InputStream.read method, which [tells you how many bytes were actually read](https://docs.oracle.com/javase/9/docs/api/java/io/FileInputStream.html#read-byte:A-). Of course, the better way to do this is `Files.readAllBytes(Paths.get("image.jpg"))`, which always reads all bytes. – VGR Oct 12 '17 at 20:21

0 Answers0