+ Start a Discussion
Srilakshmi B SSrilakshmi B S 

Creating static resource in salesforce through java code

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()];
    fin.read(fileContent);
    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();
    sr.setFullName("test11");
    sr.setCacheControl(StaticResourceCacheControl.Private);
    sr.setContentType("text/field");
    sr.setContent(encodedFile.getBytes());

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

    for (SaveResult r : results) {
        if (r.isSuccess()) {
            System.out.println("Created component: " + r.getFullName());
        } else {
            System.out
                    .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());
            }
        }
    }

    fin.close();
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.
NagaNaga (Salesforce Developers) 
Dear Srilakshmi,

base64 encoding is not recommended
Instead of StaticResource Content set fileContent[] 
WS automatically transforms your content to b64.

Please let me know if this works

Best Regards
Naga kiran