0

My requirement is to convert the blob in a database field to string to create a json object. I have achieved that.

Now, I need to convert this string back to blob. I wrote the below code. But, it does not work. In my instance, I have the word document stored as blob. I converted it to string but when I converted the string to blob, the document does not open properly.

Please let me know a way to convert the string back to blob.

DocumentTemplateKey documentTemplateKey = new DocumentTemplateKey();
documentTemplateKey.documentTemplateID = "XX";
DocumentTemplateDtls documentTemplateDtls = DocumentTemplateFactory.newInstance().read(documentTemplateKey);

byte[] blobAsBytes = documentTemplateDtls.contents.copyBytes();

ByteArrayOutputStream bos = new ByteArrayOutputStream();


bos.write(blobAsBytes, 0, blobAsBytes.length);

String pdfBase64String = 
  org.apache.commons.codec.binary.StringUtils.newStringUtf8(org.apache.
  commons.codec.binary.Base64.encodeBase64(bos.toByteArray()));

OutputStreamWriter out = new OutputStreamWriter(System.out);
JsonWriter writer = new JsonWriter(out);
//set indentation for pretty print
writer.setIndent("\t");
//start writing
writer.beginObject(); //{
writer.name("blob").value(pdfBase64String);
byte[] stringAsBytes =  org.apache.commons.codec.binary.StringUtils.getBytesUtf8(pdfBase64String);

Blob blob = new Blob(stringAsBytes);
documentTemplateDtls.contents = blob;
documentTemplateDtls.documentTemplateID = "XX12";
documentTemplateDtls.name = "XX12";
DocumentTemplateFactory.newInstance().insert(documentTemplateDtls);
writer.endObject();

writer.flush();

//close writer
writer.close();
3
  • Take a look at stackoverflow.com/a/32425109/5910038, maybe it will help. Commented Nov 8, 2018 at 8:42
  • The code is full of strange parts, like after that multi-line fight with ByteArrayOutputStream, bos.toByteArray() simply gets you an identical copy of blobAsBytes. Also, instead of the encodeBase64+StringUtils magic, you could just directly use encodeBase64String() providing a String for you directly. Note that Java itself has Base64 too, starting from Java8. Commented Nov 8, 2018 at 11:37
  • (2nd part) After that you write some JSON to the standard output, while starting to write something back to your blob, using pdfBase64String, so not a JSON. Commented Nov 8, 2018 at 11:41

1 Answer 1

0

Here's your problem:

byte[] stringAsBytes = org.apache.commons.codec.binary.StringUtils.getBytesUtf8(pdfBase64String);

You're getting the bytes of your base64 string, but you ought to be putting it through a base64 decoder.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Once I changed it to byte[] stringAsBytes = DatatypeConverter.parseBase64Binary(pdfBase64String); it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.