我需要从PostgreSQL表中读取一个图像字节数组
byte[] fileBytes = resultsSet.getBytes(1);
当我尝试将其写入图像文件时,它不会打开(不支持的文件类型)。以下是代码
FileOutputStream fos = new FileOutputStream("D:\\test.png");
fos.write(fileBytes);
fos.close();
尝试这种方法,为我工作:
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);