步骤 2 : 用法 package util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IPUtil {
public static boolean isReachable(String ip) {
try {
boolean reachable = false;
Process p = Runtime.getRuntime().exec("ping -n 1 " + ip);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
if (line.length() != 0)
sb.append(line + "\r\n");
}
reachable = sb.toString().contains("TTL");
br.close();
return reachable;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
System.out.println(IPUtil.isReachable("192.168.2.189"));
System.out.println(IPUtil.isReachable("192.168.2.189"));
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|