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
HiteshPHiteshP 

Need Help on Downloading Salesforce

I have written Java utility which download salesforce attachments but i'm stucked, my utility is downloading only one attachment. 

Can someone please look into the code and help?

File Name :ExportAttachments.java

package extractordaemon;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import javax.mail.util.ByteArrayDataSource;
import sun.misc.BASE64Decoder;

/**
 *
 * @author hitesh.patel
 */
public class ExportAttachments {

    static final String USERNAME = "<USERNAME>";
    static final String PASSWORD = "<PASSWORD>";
    static final String autURL = "https://login.salesforce.com/services/Soap/u/34.0";
    //static EnterpriseConnection connection;
    static PartnerConnection connection = null;
    static final String ATTACHIDS = "00P50000007HYMJEA4,00P50000007HmAjEAK,00P50000004CmZ3EAK";
    static final String AccountIds = "5007F00000Klxio,5007F00000KlxiS";

    static List<String> str = new ArrayList<String>();
    static List<String> stracct = new ArrayList<String>();
    static Map<String, String> idMap = new HashMap<String, String>();
    static Map<String, String> csvMap = new HashMap<String, String>();

    public static void main(String[] args) {

        ConnectorConfig config = new ConnectorConfig();
        config.setAuthEndpoint(autURL);
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);

        //config.setTraceMessage(true);
        str = Arrays.asList(ATTACHIDS.split(","));
        stracct = Arrays.asList(AccountIds.split(","));
        for (String s : stracct) {
            idMap.put(s, s);
        }
        try {

            connection = Connector.newConnection(config);
            // display some current settings
            System.out.println("Auth EndPoint: " + config.getAuthEndpoint());
            System.out.println("Service EndPoint: " + config.getServiceEndpoint());
            System.out.println("Username: " + config.getUsername());
            System.out.println("SessionId: " + config.getSessionId());

          
            queryAttachments();
          

        } catch (ConnectionException e1) {
            e1.printStackTrace();
        }

    }
    

    
    private static void queryAttachments() {
        try {
            //for(String s:idMap.keySet()){
            QueryResult queryResults = connection.query("Select Id, ParentId, Name, ContentType, Body "
                    + "From Attachment limit 10");

            if (queryResults.getSize() > 0) {
                for (int i = 0; i < queryResults.getRecords().length; i++) {
                    // cast the SObject to a strongly-typed Contact
                    SObject a = (SObject) queryResults.getRecords()[i];
                    String blob = a.getField("Body").toString().trim();

                    BASE64Decoder decoder = new BASE64Decoder();
                    byte[] imgBytes = decoder.decodeBuffer(blob);
                    writeOnDisk(a.getId() + "-" + a.getField("Name").toString().trim(), imgBytes);
                    System.out.println("I: " + i);
                }
            }
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void writeOnDisk(String fileName, byte[] bdy) {
        try {
            String filePath = "C://Users//hitesh.patel//Desktop//AUTH//att//" + fileName;

            ByteArrayDataSource rawData = new ByteArrayDataSource(bdy, "application/octet-stream");
            BufferedInputStream reader = new BufferedInputStream(rawData.getInputStream(), 4096);

            FileOutputStream fos = new FileOutputStream(filePath);//File OutPutStream is used to write Binary Contents like pictures

            BufferedOutputStream writer = new BufferedOutputStream(fos, 4096);

            byte[] buf = new byte['?'];
            int byteRead;
           // reader.read(buf, 0, 4096))
            System.out.println("reader : "+reader.read());
            while ((byteRead = reader.read()) >= 0) {
                //writer.write(buf, 0, byteRead);
                writer.write(byteRead);
            }
            reader.close();

            writer.flush();

            writer.close();

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
Best Answer chosen by HiteshP
HiteshPHiteshP
I found the bug.