我在Tomcat中运行的Web应用程序遇到了奇怪的响应文本。基本上,我的Web应用程序将调用命令中运行的ping应用程序,并将结果返回给客户端。
代码如下:
@GET
@Path("/pingport/{ip}/{port}")
public Response pingIpPort(@PathParam("ip") String ip, @PathParam("port") String port) {
String result = null;
String command = "/tmp/paping -p " + port +" -c 5 " + ip;
try {
// ProcessBuilder builder = new ProcessBuilder(
// "bash", "-c", "sudo hping3 -S -c 5 -p "+ port + " " + ip );
// builder.redirectErrorStream(true);
// Process proc = builder.start();
Process proc = Runtime.getRuntime().exec(command);
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
// System.out.print(line + "\n");
LOGGER.warning(line + "\n");
if(line.contains("Minimum")) {
result=line;
}
}
proc.waitFor();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Read the output
return Response.status(200).entity(result).build();
}
当我转到提到的URL时,它给出了奇怪的编码,如下所示:
最小值= [01; 34m21.46ms [22; 0m,最大值= [01; 34m27.20ms [22; 0m, 平均= [01; 34m24.31ms [22; 0m
但是,如果我直接从Ubuntu中的终端运行命令,它将像这样:
Minimum = 20.69ms, Maximum = 28.48ms, Average = 23.77ms
你知道出什么问题吗?