java - 从 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)
解决方案
bin 目录位于 linux 的根目录 (/) 中,因此bin/sh
您必须使用/bin/sh
Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh", command.toString()});
推荐阅读
- python - 在 pycharm 中将 .py 转换为 .ipynb 文件
- r - 在 R 中向 ggplot 添加单独的回归线
- jquery - 过滤后不加载模态
- c++ - 嵌套 MACRO 中的位操作导致“需要左值作为赋值的左操作数”,为什么?
- python - sqlalchemy,插入尝试除块
- reactjs - 尝试查询我的 mongodb 以检查用户是否存在并根据响应注册它们
- python - 如何将图像的 matplotlib.pyplot 颜色图表示转换为 RGB 图像
- php - PHP 内置 Web 服务器和路由器脚本中的语法错误
- java - 将泛型数据插入泛型类时发生错误
- angular - 将字符串推送到可观察的