我创建了一个shell脚本,它有一个curl命令将任何图像发送到服务器。 通过git bash手动运行shell脚本非常有效。
我需要使用java代码在Linux机器上执行这个shell脚本。但是当我运行它时,我在输出中看到以下错误:
获取错误:警告:无法从文件" image.jpg"中读取数据,这会导致空POST。
Java代码:
String filePath=directoryPath + "/APIData/email_template.sh";
File sendImage = new File(filePath);
if(!sendImage.isFile()){
throw new IllegalArgumentException("The file " + filePath + " does not exist");
}
String[] command = {"/bin/bash","-c", directoryPath + "/APIData/email_template.sh", "argument1","argument2"};
ProcessBuilder p = new ProcessBuilder(command);
Process proc = p.start();
String line;
outputFile = new File(directoryPath + "/target/response.txt");
BufferedReader read = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
fos = new FileOutputStream(outputFile);
bw = new BufferedWriter(new OutputStreamWriter(fos));
String s;
while ((s = read.readLine()) != null) {
bw.write(s);
}
read.close();
bw.close();
这是email_template.sh文件
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
FIRST=$1
SECOND=$2
curl -v -X POST "https://ipaddress-host/$FIRST/template?mimeType=image/png" -H "Content-Type: application/octet-stream" -k --data-binary "@image.png" -H "Authorization: $SECOND"
image.png存在于.sh文件所在的同一文件夹中。我为什么要
"警告:无法从文件" image.jpg"中读取数据,这会导致空POST。"
答案 0 :(得分:1)
在
Process proc = p.start();
插入
p.directory( new File( directoryPath, "APIData" ));
的文档