简体   繁体   中英

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

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

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"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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