我之前从未使用过curl
。我尝试运行命令并在Web应用程序中解析XML结果。
curl -k https://ip.address.example --data-urlencode file.xml
所以我的选择仅限于:
Runtime.getRuntime.exec
jQuery.ajax()
我试过这个:
$.ajax({
url : "https://ip.address.example",
type : 'GET',
dataType : 'json',
contentType : 'application/json',
file: "file.xml",
success : function(data) {
console.log(JSON.stringify(data));
},
error : function() {
console.log("Cannot get data");
}
});
但它似乎不起作用。
对于第一种方法,我尝试过:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Gson gson = new GsonBuilder().serializeNulls().create();
Process p = Runtime.getRuntime().exec("curl -k https://ip.address.example --data-urlencode file.xml");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line , output = "";
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
System.out.println(output);
out.printf(gson.toJson(output));
} catch (Exception e) {
e.printStackTrace();
}
}
但我在out.printf(gson.toJson(output));
java.util.UnknownFormatConversionException: Conversion = ')'
任何建议?
答案 0 :(得分:0)
解决方案是: 替换
out.printf(gson.toJson(output));
out.printf(output);