import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CurlTest {
public static void main(String[] args){
String[] cmds={"curl", "-u", "root:iHJyrW3eZA", "-X", "POST", "-H", "Content-Type:application/json", "-d", "@sqltest.json", "http://127.0.0.1:18888/druid/v2/sql"};
System.out.println(execCurl(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;
}
}
PS:json文件(sqltest.json)需要放到工程目录下