0

I need to read an image byte array from a PostgreSQL table which is done as

byte[] fileBytes = resultsSet.getBytes(1);

When I try to write it to a image file, it does not open (unsupported file type). The following is the code

FileOutputStream fos = new FileOutputStream("D:\\test.png"); 
fos.write(fileBytes);
fos.close();
S_S
  • 1,276
  • 4
  • 24
  • 47

1 Answers1

0

Try this method, worked for me:

      InputStream in = new ByteArrayInputStream(rs.getBytes(1));
      BufferedImage bImageFromConvert = ImageIO.read(in);
                    if(null == bImageFromConvert){
                        throw new Exception("Invalid image");
                    }
                    OutputStream out = new FileOutputStream("e:/images/"+"test".jpg");

        ImageIO.write(bImageFromConvert, "png", out);
Shariq
  • 513
  • 4
  • 14