From a DB2 table I've got blob which I'm converting to a byte array so I can work with it. I need to take the byte array and create a PDF
out of it.
This is what I have:
static void byteArrayToFile(byte[] bArray) {
try {
// Create file
FileWriter fstream = new FileWriter("out.pdf");
BufferedWriter out = new BufferedWriter(fstream);
for (Byte b: bArray) {
out.write(b);
}
out.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
But the PDF
it creates is not right, it has a bunch of black lines running from top to bottom on it.
I was actually able to create the correct PDF
by writing a web application using essentially the same process. The primary difference between the web application and the code about was this line:
response.setContentType("application/pdf");
So I know the byte array is a PDF
and it can be done, but my code in byteArrayToFile
won't create a clean PDF
.
Any ideas on how I can make it work?
Sending your output through a FileWriter
is corrupting it because the data is bytes, and FileWriter
s are for writing characters. All you need is:
OutputStream out = new FileOutputStream("out.pdf");
out.write(bArray);
out.close();