从FTP获取数据并添加salesforce

时间:2019-06-14 06:35:55

标签: java ftp

我正在尝试使用数据加载器将数据从ftp发送到Salesforce,但是面临以下问题。

java.io.IOException:无法运行程序“ bin / sh”:CreateProcess错误= 2,系统找不到指定的文件

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class InvoiceFTPtoSF {

    final String rootDirectory = "C:/abc/def/dataloader/";
    int read;
    public void executeFTPtoSF(String filename){
        SimpleDateFormat format = new SimpleDateFormat("dd_MM_yyyy_HHmmss");
        String date = format.format(new Date());
        StringBuffer command  = new StringBuffer();
        command.append("java -cp \"").append("dataloader-45.0.0-uber.jar\" ");
        command.append("-Dsalesforce.config.dir=").append(rootDirectory).append("samples/conf com.salesforce.dataloader.process.ProcessRunner process.name=\"Test\" ");
        command.append("process.encryptionKeyFile=\"").append(rootDirectory).append("key.txt\" ");
        command.append("dataAccess.name=\"").append(rootDirectory+"FTP_Files/").append(filename).append("\" ");
        command.append("process.mappingFile=\"").append(rootDirectory).append("samples/conf/TestdataMap.sdl\" " );

        try{
            Process p = Runtime.getRuntime().exec(new String[] {"bin/sh", command.toString()});
            BufferedInputStream catOutput= new BufferedInputStream(p.getInputStream());
            read = 0;
            byte[] output = new byte[1024];
            while ((read = catOutput.read(output)) != -1) {
            }   
            if(p.waitFor() == 0){
                try {
                    int totalRows = (CSVUtils.getNumberOfRowsInCSV(rootDirectory+"FTP_Files/abc.csv") - 1) ;
                    StringBuffer body = new StringBuffer();
                    body.append("The file name processed is :").append("abc.csv").append(" \n ");
                    body.append("Total No. of rows in file is : ").append(totalRows).append(" \n ");
                             moveFiletoBackup("abc.csv");
                } catch (Exception e) {}
            }else{
                System.out.println("not waited ...completed");
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public void FTPtoSF(){
        FTPUtil.downloadFilesFromFTP("abc.csv");
        File file = new File(rootDirectory+"FTP_Files/abc.csv");
        if(!file.isDirectory()){
            executeFTPtoSF(file.getName());             
        }
    }

    public void moveFiletoBackup(String filepath){
        System.out.println("Backup File");
        InputStream inStream = null;
        OutputStream outStream = null;
        try{

            File afile =new File(rootDirectory+"FTP_Files/Invoice_File/abc.csv");
            File bfile =new File(rootDirectory+"FTP_Files/processed_files/Invoice_File/abc.csv");
            System.out.println("Bfile"+bfile);
            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inStream.read(buffer)) > 0){
                outStream.write(buffer, 0, length);
            }
            inStream.close();
            outStream.close();
            afile.delete();

        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        try{
            new InvoiceFTPtoSF().FTPtoSF();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

预期的输出:应从ftp下载的文件中将数据填充到salesforce对象中。

Actual Result: 
java.io.IOException: Cannot run program "bin/sh": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at InvoiceFTPtoSF.executeFTPtoSF(InvoiceFTPtoSF.java:29)
    at InvoiceFTPtoSF.FTPtoSF(InvoiceFTPtoSF.java:74)
    at InvoiceFTPtoSF.main(InvoiceFTPtoSF.java:105)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)

1 个答案:

答案 0 :(得分:0)

bin目录位于linux的根目录(/)中,因此必须使用bin/sh来代替/bin/sh

Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh", command.toString()});