function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

I am facing problems with uploading pdf

Here is the whole code:->
private String createDocument(String content)
        try {
        //    getUserInput("Hit enter to upload file......");
            //Retrieve Folder for file upload
            Folder folder = null;
            QueryResult qr = binding.query("Select Id, Name from Folder Where Name = 'CRDocuments'");  //the folder name where you would like the text file to be saved.
            if (qr.getSize() > 0)
                System.out.println("The folder size is greater than zero");
                folder = (Folder) qr.getRecords(0);
            System.out.println("folder name id:" + folder.getName());
            System.out.println("folder id is:"+folder.getId());
            //Create document object
            if(folder != null)
                com.sforce.soap.enterprise.sobject.Document document = new com.sforce.soap.enterprise.sobject.Document();
                document.setName("InvoiceData");                //the name of the file
                documents = new com.sforce.soap.enterprise.sobject.Document[1];
                documents[0] = document;
                // create the object(s) by sending the array to the web service
                SaveResult[] sr = binding.create(documents);
                System.out.println("The number of the document is:"+sr.length);
                System.out.println("The body length is:"+document.getBodyLength());
                System.out.println("The text in the body is:"+document.getBody());
                for (int j = 0; j <sr.length; j++)
                    if (sr[j].isSuccess())
                        System.out.println("A document was created with an id of: " + sr[j].getId());
                        return sr[j].getId();
                        for (int i = 0; i < sr[j].getErrors().length; i++)
                            com.sforce.soap.enterprise.Error err = sr[j].getErrors()[i];
                            System.out.println("Errors were found on item " + new Integer(j).toString());
                            System.out.println("Error code is: " + err.getStatusCode().toString());
                            System.out.println("Error message: " + err.getMessage());
                        return null;
            //    getUserInput("\nHit return to continue...");
        } catch (ApiFault af) {
                    .println("\nFailed to create account, error message was: \n"
                            + af.getExceptionMessage());
            //getUserInput("\nHit return to continue...");
        } catch (Exception ex) {
                    .println("\nFailed to create account, error message was: \n"
                            + ex.getMessage());
            //getUserInput("\nHit return to continue...");
        return null;

Is it because of the content in the body that the error is generated.The mime type is set as application/pdf what more do i need to do???In the case of jpeg files too I am facing the same problem.A document with no contents gets made.I am pretty sure its related to the content in the body.Kindly could you point out my mistakes.
I get the error like:->
File does not begin with '%PDF-'

What values should I pass in contents.Is the content type that i am using correct??

the document body should be the bytes of the file contents. you seem to be setting it to the bytes of the filename? what are you passing as content in your calls to this method.
Thank you for the help.The main problem that I am facing is that the code runs perfectly while I do it in my Eclipse.When I try to use the html code i get this as an exception:->
Exception in thread "AWT-EventQueue-26" java.lang.NoClassDefFoundError: Could not initialize class org.apache.axis.client.AxisClient
at org.apache.axis.client.Service.getAxisClient(
at org.apache.axis.client.Service.(
at com.sforce.soap.enterprise.SforceServiceLocator.(
at UploadingPDF.login(
at UploadingPDF.actionPerformed(
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at Source)
which is casused by access denied
(java.lang.RuntimePermission createClassLoader)

When I go thorugh the java code for logging in I find the error is here but I have no clue whether to make head or tail out of it:->
System.out.println("Creating the binding to the web service...");

* There are 2 ways to get the binding, one by passing a url to the
* getSoap() method of the SforceServiceLocator, the other by not
* passing a url. In the second case the binding will use the url
* contained in the wsdl file when the proxy was generated.
try {
binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
.println("The login url is: "
+ binding
} catch (ServiceException ex) {
.println("ERROR: creating binding to soap service, error was: \n"
+ ex.getMessage());
System.out.print("Hit return to continue...");
return false;

// Time out after a minute
The jar is signed and all the files are present.