java_获取访问者IP_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java_获取访问者IP

java_获取访问者IP

 2014/6/10 13:33:12  tzz6  程序员俱乐部  我要评论(0)
  • 摘要:publicStringgetIp(HttpServletRequestrequest){Stringip=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
  • 标签:Java
class="java">public String getIp(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.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 ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)){
	        try {
	            ip= InetAddress.getLocalHost().getHostAddress();
	        }
	        catch (UnknownHostException e) {
	        	e.printStackTrace();
	        }
	    }
	return ip
}

?

?

发表评论
用户名: 匿名