|
楼主 |
发表于 2018-11-9 17:25:30
|
显示全部楼层
public static String[] dnsipList=new String[]
{
"114.114.114.114",
"114.114.115.115",
"1.2.4.8",
"210.2.4.8",
"101.226.4.6",
"123.125.81.6",
"8.8.8.8",//googleDNS
"8.8.4.4",//googleDNS 后备
};
// ipAddress可以是IP或者是域名如:183.232.231.173或者www.baidu.com
public static boolean ping(String ipAddress) {
int timeOut = 3000; // 超时应该在3钞以上
boolean status = false;
try {
status = InetAddress.getByName(ipAddress).isReachable(timeOut);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
log4j.error(e);
} catch (IOException e) {
// TODO Auto-generated catch block
log4j.error(e);
} // 当返回值是true时,说明host是可用的,false则不可。
return status;
}
static boolean isOnline()
{
for (int i=0; i<dnsipList.length; i++)
{
if (ping(dnsipList))
{
return true;
}
}
return false;
}
|
|