我希望ping命令输出显示在网页上的textarea中。我有我的控制器方法:
@RequestMapping(method = RequestMethod.POST, value = "testInternet.html")
@ResponseBody
public String testInternetConnection() {
String result = "error";
String destinationIpAddress = "192.168.1.64";
//String command = "ping -c 5 ";//for linux
String command = "ping ";
String content = "";
try {
Process p = Runtime.getRuntime().exec(command+destinationIpAddress);
BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "";
while ((s = inputStream.readLine()) != null) {
//System.out.println("sending "+s);
//return s;
content = content + s + System.lineSeparator();
}
System.out.println("Result "+content);
return content;
} catch (Exception e) {
result = "error";
e.printStackTrace();
}
return result;
}
我的Ajax jquery调用是这样的
$('#testInternet').on('click', function(event) {
event.preventDefault();
console.log("inside test internet Connection");
$.ajax({
url : 'testInternet.html',
type : 'POST',
processData : false,
async : false,
contentType : "application/json; charset=utf-8",
success : function(result) {
console.log(result);
$('#ip_output').val(result);//setting output from controller to textarea
var nodeList = document.querySelectorAll('.mdl-textfield');
Array.prototype.forEach.call(nodeList,function(elem) {
elem.MaterialTextfield.checkDirty();
});
},
error : function(jqXHR, textStatus, errorThrown) {
alert('error');
}
});
});
这里我现在得到完整的输出现在我想要从控制器逐行输出,以便在textarea中附加,就像它在命令提示符上显示一样。所以任何人都可以帮我解释如何实现这一点。