java apache IOUtils breaks file content - java

I need to encode/decode pdf file into Base64 format.
So I read file from disk into String(because I will receive file in String Base64 format in future);
String pdfString = IOUtils.toString(new FileInputStream(new
File("D:\\vrpStamped.pdf")));
byte[] encoded = Base64.encodeBase64(pdfString.getBytes());
byte[] newPdfArray = Base64.decodeBase64(encoded);
FileOutputStream imageOutFile = new FileOutputStream(
"D:\\1.pdf");
imageOutFile.write(newPdfArray);
imageOutFile.close();
imageOutFile.flush();
So my D:\\1.pdf doesnt opens in AdobeReader, but if I read file straight to byte array, using IOUtils.toByteArray(..) instead ,all works fine and my D:\\1.pdf file sucessfuly opens in Adobe Reader:
byte[] encoded = Base64.encodeBase64(IOUtils.toByteArray(new FileInputStream(new File("D:\\vrpStamped.pdf"))););
It seems to me thath IOUtils.toString(..) change something inside file content. So how can I convert file to String with not content breaking?

How to encode a pdf...
byte[] bytes = IOUtils.toByteArray(new FileInputStream(new File("/home/fschaetz/test.pdf")));
byte[] encoded = Base64.encode(bytes);
String str = new String(encoded);
...now do something with this encoded String, for example, send it via a Rest service.
And now, if you receive an encoded String, you can decode and save it like this...
byte[] decoded = Base64.decode(str.getBytes());
FileOutputStream output = new FileOutputStream(new File("/home/fschaetz/result.pdf"));
output.write(decoded);
output.close();
Works perfectly fine with all files, not limited to images or pdfs.
What your example is doing is...
Read the pdf into a String (which pretty much destroys the data, since you are reading binary data into a String)
Encode this spring (which is in all likelyhood not a valid representation of the original pdf anymore)
Decode it and save it to disk

Related

Java Base64 encoding of Excel File missing last byte

I am base64 encoding an excel file, send it somewhere where it is saved. Apparently after this, excel complains that the file is incorrect and if I want to attept a restore. The code I am doing (actually I did a quick test main method) is:
public static void main(String[] args) throws IOException {
Path p = Paths.get("C:\\VariousJunk\\excel-test", "test.xlsx");
ByteArrayOutputStream base64StringOutputStream = new ByteArrayOutputStream();
OutputStream base64EncodingStream = Base64.getEncoder().wrap(base64StringOutputStream);
Files.copy(p, base64EncodingStream);
base64StringOutputStream.close();
String b64 = base64StringOutputStream.toString();
byte[] data = Base64.getDecoder().decode(b64);
FileOutputStream fos = new FileOutputStream("C:\\VariousJunk\\excel-test\\test-backup.xlsx");
fos.write(data);
fos.close();
}
Now I have compared binary data of both files and it appears, that the output file is only missing one last byte with value 0. I have added the last bit for the test
fos.write(data);
fos.write(0);
fos.close();
And it works fine. The problem is I will be using this for any other type of data, and so I am not sure whether hardcoding a last byte is a good idea, possibly it might crash other filetypes. Is this a feature of this Base64 encoding method or am I doing something wrong?
Apparently the missing bit was base64EncodingStream.close() just after Files.copy()
OutputStream base64EncodingStream = Base64.getEncoder().wrap(base64StringOutputStream);
Files.copy(p, base64EncodingStream);
base64EncodingStream.close();
String b64 = base64StringOutputStream.toString();

Junk Characters Coming while Decoding Data in Base 64

I am getting PDF content which is Base 64 encoded. I tried to decode it using NIFI with Processor Base64EncodeContent. The Decoded file I am sending in mail. Below is small sample of output coming in mail.
enter image description here
"No data should be available in . ¹ Check if sent . . All documents are sent as pdf to* 9 : ’ ³: > < âA m¬‘²#%é‚ÇŽÇ¢|ÀÈ™$Éز§Uû÷LÒTB¨ l,îåù˜$â´º?6N¬JC¤ŒÃ°‰_Ïg -æ¿;ž‰ìÛÖYl`õ?èÓÌ[ ÿÿ PK"
How to extract the data in PDF as sent by third party?
I have tried to decode it using JAVA code and there also its failing. Not able to open the PDF, junk characters coming there too.
ConvertedJPGPDF.pdf file used below contains Base64 encoded String.
String filePath = "C:\\Users\\xyz\\Desktop\\";
String originalFileName = "ConvertedJPGPDF.pdf";
String newFileName = "test.pdf";
byte[] input_file =
Files.readAllBytes(Paths.get(filePath+originalFileName));
// byte[] decodedBytes = Base64.getDecoder().decode(input_file);
byte[] decodedBytes1 = Base64.getMimeDecoder().decode(input_file);
FileOutputStream fos = new FileOutputStream(filePath+newFileName);
fos.write(decodedBytes1);
fos.flush();
fos.close();
You mentioned that the file contains base64 encoded string already.
ConvertedJPGPDF.pdf file used below contains Base64 encoded String.
So, you don't need to run this line:
byte[] encodedBytes = Base64.getEncoder().encode(input_file);
By doing so, you are trying to encode those bytes again.
Directly decode the input_file array and then save the obtained byte array into a .pdf file.
Update:
The ConvertedJPGPDF.pdf doesn't really have to be named .pdf. It's really a plain text file considering that it is base 64 encoded.
Anyway, the following piece of code is working for me:
String filePath = "C:\\Users\\xyz\\Desktop\\";
String originalFileName = "ConvertedJPGPDF.pdf";
String newFileName = "test.pdf";
byte[] input_file = Files.readAllBytes(Paths.get(filePath+originalFileName));
byte[] decodedBytes1 = Base64.getMimeDecoder().decode(input_file);
Files.write(Paths.get(filePath+newFileName), decodedBytes1);
Hope this helps!

Issues in converting base64 decoded byte array to String in java

Issues in converting base64 decoded byte array to String in java :
public static String decode(String strcontent) throws Exception
{
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(strcontent);
return new String(imgBytes);
}
With the above code; was trying to create a string out of the Base 64 decoded byte array (imgBytes ) & input strcontent is base 64 encoded string. For text files it working fine , but for PDF and image files the string conversion is having issues. Have tried different encoding as UTF-8 , UTF 16 etc. But no use. The returned string is different than the original one.
When tried to write the byte array to a file like :
OutputStream out = new FileOutputStream(##path);
out.write(imgBytes);
out.close();
File is getting created properly without any issues.
I tried the below code:
byte[] imgBytes= ( new String(imgBytes1)).getBytes(); //Converting to String and back to bytes
OutputStream out = new FileOutputStream(##Filename);
out.write(imgBytes); out.close();
This time the image file is corrupted.
Please suggest.

Convert string from xml response to pdf

I'm getting a response XML from web service and one of the field contains a string that should be convertable to pdf file.
This is example string that I extract from xml response:
JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDwvUHJvY1NldFsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldL0ZvbnQ8PC9GMSA2IDAgUi9GMiA3IDAgUj4+Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDg0MS44OSA1OTUuMjhdL01hdHJpeFsxIDAgMCAxIDAgMF0vTGVuZ3RoIDQwNTYvRm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RleHQgMi4xLjcgYnkgMVQzWFQpL01vZERhdGUoRDoyMDE1MDMwOTEwMzcwOSswMScwMCcpL0NyZWF0aW9uRGF0ZShEOjIwMTUwMzA5MTAzNzA5KzAxJzAwJyk+PgplbmRvYmoKeHJlZgowIDE0CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwNDU5OSAwMDAwMCBuIAowMDAwMDA3OTY3IDAwMDAwIG4gCjAwMDAwMDgxMjkgMDAwMDAgbiAKMDAwMDAwNDQ4MiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDQzMDEgMDAwMDAgbiAKMDAwMDAwNDM5NCAwMDAwMCBuIAowMDAwMDA3ODUwIDAwMDAwIG4gCjAwMDAwMDQ3NjEgMDAwMDAgbiAKMDAwMDAwNzY2NyAwMDAwMCBuIAowMDAwMDA3NzYxIDAwMDAwIG4gCjAwMDAwMDgxOTggMDAwMDAgbiAKMDAwMDAwODI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8L1Jvb3QgMTIgMCBSL0lEIFs8NGYyYjMyZmU2NjQ4MzBlZjYxODliMzJhMjNiY2E1MTg+PDIwZGUyZmJkMjc1NzVhMzMyNmNkYTQ0OGNmYmE0OTMyPl0vSW5mbyAxMyAwIFIvU2l6ZSAxND4+CnN0YXJ0eHJlZgo4MzY3CiUlRU9GCg==
now I'm trying to convert it into proper pdf file like this:
byte[] bytes = THAT_LONG_STRING.getBytes();
BufferedOutputStream bos = null;
FileOutputStream fos = new FileOutputStream(new File("a.pdf");
bos = new BufferedOutputStream(fos);
bos.write(bytes);
bos.flush();
bos.close();
Sadly this does not work for me, output file is just file with that long string, pdf viewers cannot open it.
Any advices are welcome :)
Your string is in Base64 format. You should decode it to a byte array first, then write it to a file.
Just replace this line:
byte[] bytes = THAT_LONG_STRING.getBytes();
With this one (Apache Commons Codec required):
byte[] bytes = Base64.decodeBase64(THAT_LONG_STRING);

How to receive the encoded byte array in rest webservice and decode it and save as image in java

I am trying to get encoded byte of image into base64 format and sending to rest webservice. Inside the service i want to decode it and convert it back to image and want to save the image in my local path. Its fails during decoding. I dont know whether the logic is correct. Need some guidance
#POST
#Path("/imageupload")
#Produces("text/html")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String execute(#FormDataParam("image") String inputfile) throws Exception
{
System.out.println("entered into service");
BASE64Decoder decoder = new BASE64Decoder();
ByteBuffer img=decoder.decodeBufferToByteBuffer(inputfile);
System.out.println();
File outputfile = new File("D:\\TEST\\test.png");
ImageIO.write(img, "png", outputfile);
The logic is:
Step 1: get the byte array as string inside the service.
Step 2: decode it
Step 3: write the byte into file with png format
Provided you get correctly encoded image and you are sure it is png, I would do it this way.
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedImageBytes = decoder.decodeBuffer(inputfile);
final FileOutputStream out = new FileOutputStream(new File("D:\\TEST\\test.png"));
out.write(decodedImageBytes);
out.close();
P.S. Make sure you receive the image as encoded string.

Categories

Resources