Derrick Peh
Derrick Peh

Reputation: 309

Base64 String to Json String

I currently need to send a PDF file through an API by converting it to a Base64 string. I have tested my code and realize that it cannot accept the contents in my pdfBase64String variable.

I tried printing the pdfBase64String and it does not show up anything. I tried decoding back to a pdf file and it works so the problem should lie in the String itself. Is there anyway i can solve this problem? i would still want to send it in string and the other party who receives it will decode it back to the pdf file.

ByteArrayOutputStream ba = loadPdf(fileName);
String pdfBase64String = StringUtils.newStringUtf8(Base64.encodeBase64(ba.toByteArray()));
System.out.println(pdfBase64String);     //Does not show anything

ApprovalReport approvalReport = new ApprovalReport(Long.valueOf(crisisID), pdfBase64String);
URI uri = restTemplate.postForLocation(CMO_SERVICE_URI + "/approvalReport/", approvalReport, ApprovalReport.class);

Upvotes: 1

Views: 7809

Answers (1)

tune5ths
tune5ths

Reputation: 723

You can use Base64.encodeBase64String:

            String pdfBase64String = Base64.encodeBase64String(ba.toByteArray());

Upvotes: 3

Related Questions