I am trying to create static resource in salesforce through java code. I am fetching the content of a file which is stored in my system and encoding it into base64 format and putting that content into the content of static resource. The code snippet is as below:

    File file = new File("D:/Personal/salesforce_sharing_cheatsheet.pdf");

    FileInputStream fin = new FileInputStream(file);

    byte fileContent[] = new byte[(int)file.length()];
    System.out.println("fileContent: " +fileContent);
    String fileContentString = new String(fileContent);
    System.out.println("fileContentString: " +fileContentString);

    byte[] encoded = Base64.encodeBase64(fileContent);
    String encodedFile = new String(encoded);
    System.out.println("encoded: " +encoded);
    System.out.println("encodedFile: " +encodedFile);
    System.out.println("encodedFile.getBytes(): " +encodedFile.getBytes());

    StaticResource sr = new StaticResource();

    SaveResult[] results = metadataConnection.createMetadata(new Metadata[] { sr });

    for (SaveResult r : results) {
        if (r.isSuccess()) {
            System.out.println("Created component: " + r.getFullName());
        } else {
                    .println("Errors were encountered while creating "
                            + r.getFullName());
            for (Error e : r.getErrors()) {
                System.out.println("Error message: " + e.getMessage());
                System.out.println("Status code: " + e.getStatusCode());


The static resource is getting created in salesforce but the content is appearing as encoded data.

How can I get the original content in the created static resource. Please help me.

  • not sure if this is the only issue but you may want to try and set correct content type. Instead of sr.setContentType("text/field"); try sr.setContentType("application/pdf"); – gaiser Mar 25 '15 at 13:08
  • I tried by setting content type as "application/pdf". Now when i click on created static resource i am getting the error: 'Failed to load PDF document'. Both static resource of type "text/field" and "application/pdf" are of same size. – Srilakshmi B S Mar 25 '15 at 13:26
  • out of interest - why do you do: String encodedFile = new String(encoded); and then sr.setContent(encodedFile.getBytes()); instead of sr.setContent(encoded);. Probably won't make a difference, but I would try setting encoded directly, without conversion to string and back. – gaiser Mar 25 '15 at 13:48
  • Yes. Both are same. Still I am not able to get the content. – Srilakshmi B S Mar 25 '15 at 14:37

I checked your code and figured out what is wrong.
You shouldn't do base64 encoding.
Instead as StaticResource Content set fileContent[]
WS automatically transforms your content to b64. That's it

Edit: One more thing, you should set content type as application/pdf sr.setContentType("application/pdf")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.