2

I am having problem when importing document (PDF) into Alfresco repository inside java backed webscript. I am using writer of ContentService. If I use

ContentWriter writer = ContentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
writer.setEncoding("UTF-8");
writer.setMimetype("application/pdf");
writer.putContent(new String(byte []) );

or

writer.putContent(new String(byte [], "UTF-8") );

my document is not previewable (I see blank PDF file, tried with few small PDF files, don't know what would happen in case of other/larger files). But if I use another putContent method which takes File as argument I'll successfully import the document.

writer.setEncoding("UTF-8");
writer.setMimetype("application/pdf");
writer.putContent(File);

I don't want to import file from disk since I get the file as Base64 encoded String but I don't know what am I missing.

3
  • 1
    is that your code? writer.putContent(new String(byte []) ); maybe you could try putContent with an InputStream... What are you doing with the "Base64 encoded String"? Commented Mar 24, 2016 at 14:34
  • Sorry I tried to omit variable name, I'll edit the question to make it more clear. TBH I didn't tried to use InputStream as argument, I'll try. I decode the stream using Apache Base64.decodeBase64(). It goes well since I use that output to generate file (used for import in second scenario I wrote in question). Commented Mar 24, 2016 at 14:52
  • Nice noticing Meiko, it gets in repostiory just fine when using InputStream as an argument. I sure don't know what went wrong when creating a string from byte array. Thanks a lot. Please write an answer and I'll upvote it. Commented Mar 24, 2016 at 15:11

1 Answer 1

4

You could use an InputStream as a parameter for ContentWriter::putContent. So you will prevent the String to byte array (and vice versa) conversions, which leads to difficulties with the encoding.

writer.putContent(new ByteArrayInputStream(Base64.decodeBase64("yourBase64EncodedString")))
Sign up to request clarification or add additional context in comments.

Comments

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.