[url]http://blog.youkuaiyun.com/ligaoyang/article/details/8029020[/url]
public String ping(String ip) {
try {
String command = "ping "+ip;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
return out+error;
} catch (Exception e) {
log.error("ping task failed.",e);
return e.toString();
}
}

本文介绍了一种使用Java实现的ping命令,通过调用操作系统命令ping来检测目标IP的可达性,并将输出结果以字符串形式返回。
1625

被折叠的 条评论
为什么被折叠?



