首页 > 解决方案 > 从 FTP 获取数据并添加到 salesforce

问题描述

我正在尝试使用 dataloader 将数据从 ftp 发送到 salesforce,但面临以下问题。

java.io.IOException: Cannot run program "bin/sh": CreateProcess error=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)

标签: javaftp

解决方案


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

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

推荐阅读