2 zcs100 zcs100 于 2013.10.09 20:32 提问

java根据ip判断网络连接情况(i除Java执行ping命令和InetAddress这两种方式)
  因为ava执行ping命令和InetAddress这两种方式执行速度太慢了,有没有更好的解决方法???  

3个回答

u011575463
u011575463   2013.10.11 15:58
已采纳

获得客户端真实IP地址的方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}   
获得客户端真实IP地址的方法二:
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}

abeautifulboy
abeautifulboy   2013.10.11 16:57

ip一般都是路由等网络设备分配的,有ip不代表就一定能访问公网吧,还不如直接访问百度一下试试网络通不通呢,呵呵,直接获取http头就行了

lucumu
lucumu   2013.10.15 14:08

opennms有个 jicmp组件,你可以试一下

Csdn user default icon
上传中...
上传图片
插入图片