Java执行curl命令
2021-04-21 00:29
标签:adl 数组 curl命令 readline nts tac NPU 而且 inpu 需要注意的是,命令需要以数组的形式传递参数,就是把正常的命令以空格切分成数组就行了。而且参数前后不能有空格,不然会报错。 具体的测试代码如下: Java执行curl命令 标签:adl 数组 curl命令 readline nts tac NPU 而且 inpu 原文地址:https://www.cnblogs.com/jimlau/p/13282888.htmlJava执行curl命令
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestCurl {
public static void main(String[] args){
String[] cmds={"curl","http://localhost:18631/rest/v1/system/info"};//必须分开写,不能有空格
System.out.println(execCurl(cmds));
}
//这里执行cmds命令
public static String execCurl(String[] cmds) {
ProcessBuilder process = new ProcessBuilder(cmds);
Process p;
try {
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
return builder.toString();
} catch (IOException e) {
System.out.print("error");
e.printStackTrace();
}
return null;
}
}