java完整版本获取客户端有效IP_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java完整版本获取客户端有效IP

java完整版本获取客户端有效IP

 2013/7/13 0:14:42  yuqihengsheng  程序员俱乐部  我要评论(0)
  • 摘要://各种奇葩问题如下,考虑IPv4与IPv6publicstaticStringgetIpAddr(HttpServletRequestrequest)throwsUnknownHostException{Stringip=request.getHeader("X-Forwarded-For");if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getHeader("Proxy-Client-IP")
  • 标签:Java 有效 客户 客户端 版本
class="java" name="code">//各种奇葩问题如下,考虑IPv4与IPv6
public static String getIpAddr(HttpServletRequest request) throws UnknownHostException {  
        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.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        if(ip != null && ip.equalsIgnoreCase("0:0:0:0:0:0:0:1"))    
        {  
          ip =InetAddress.getLocalHost().getHostAddress().toString();  
        }  
	if (ip == null || ip.length() == 0 || "X-Real-IP".equalsIgnoreCase(ip)) {  
		ip = request.getHeader("X-Real-IP");  
	}
        return ip;  
    }  

?

发表评论
用户名: 匿名