简体   繁体   English

如何从BLOB写入PNG图像文件?

[英]How to write PNG image file from BLOB?

I got this code to create and write a file from Oracle BLOB to PNG image file 我得到了这段代码来创建一个文件并将其从Oracle BLOB写入PNG图像文件

try {
    final File file = new File("C:/Users/John.Doe/Desktop/PDF Templates/POC/output/" + pdfObject.getFileName());
    final FileWriter fileWriter = new FileWriter(file);

    fileWriter.write(pdfObject.getContent());  << a String of the PNG content, see below:
    fileWriter.flush();
    fileWriter.close();
} catch (final IOException e) {
    e.printStackTrace();
}

PNG File content: PNG文件内容:

�PNG

IHDR<binary code> .....

After i'm running this code, the image is not created well, means that, when i'm trying to open it using Microsoft Photos it says: "It looks like we don't support this file format" 运行此代码后,图像创建得不好,这意味着,当我尝试使用Microsoft Photos打开Microsoft Photos显示:“看来我们不支持此文件格式”

Also, the original file size is less then the new one that being created with the code above. 同样,原始文件的大小小于使用上述代码创建的新文件的大小。

What i'm doing wrong here ? 我在这里做错了什么?

A png image is not human readable text, and it gets corrupted if you try to represent it as a string and use FileWriter. png图像不是人类可读的文本,如果尝试将其表示为字符串并使用FileWriter,则会损坏该图像。

Instead your should get the content of the blob as an array of bytes or as an InputStream, and use FileOutputStream to write it to a file. 相反,您应该以字节数组或InputStream的形式获取Blob的内容,并使用FileOutputStream将其写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM