我创建了一个带有 curl 命令的 shell 脚本,用于将任何图像发送到服务器。 通过 git bash 手动运行 shell 脚本效果很好。
我需要使用 java 代码在 Linux 机器上执行这个 shell 脚本。但是当我运行它时,我在输出中看到以下错误:
Getting error: Warning: Couldn't read data from file "image.jpg", this makes an empty 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 文件位于同一文件夹中。为什么我得到 p>
“警告:无法从文件“image.jpg”中读取数据,这会生成一个空 POST。”
最佳答案
之前
Process proc = p.start();
插入
p.directory( new File( directoryPath, "APIData" ));
关于java - 警告 : Couldn't read data from file "image.jpg", 这会导致空 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752089/