【问题标题】:Java read byte array from PostgreSQL and write to image fileJava 从 PostgreSQL 读取字节数组并写入图像文件
【发布时间】:2019-07-27 22:27:29
【问题描述】:

我需要从一个 PostgreSQL 表中读取一个图像字节数组,这样做是为了

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

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

FileOutputStream fos = new FileOutputStream("D:\\test.png"); 
fos.write(fileBytes);
fos.close();

【问题讨论】:

  • "D:\\test.png" 带有双反斜杠;你写了一个 tab+"est.png"
  • @JoopEggen 抱歉打错了。

标签: java postgresql image fileoutputstream bytea


【解决方案1】:

试试这个方法,对我有用:

      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);

【讨论】:

  • 我收到invalid image 异常,即使in 不为空。当我尝试调用ImageIO.read() 时,它会返回null
  • 嘿,你找到解决办法了吗?
  • 还没有,如果您有任何其他建议,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多