Java从PostgreSQL读取字节数组并写入映像文件

问题描述 投票:0回答:1

我需要从PostgreSQL表中读取一个图像字节数组

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

当我尝试将其写入图像文件时,它不会打开(不支持的文件类型)。以下是代码

FileOutputStream fos = new FileOutputStream("D:\\test.png"); 
fos.write(fileBytes);
fos.close();
java postgresql image fileoutputstream bytea
1个回答
0
投票

尝试这种方法,为我工作:

      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);
© www.soinside.com 2019 - 2024. All rights reserved.