当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

根据HttpServletRequest取得ip

发表于: 2013-01-21   作者:dreamoftch   来源:转载   浏览:
IP
摘要: public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCas
public static 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.getHeader("Cdn-Src-Ip");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}


如果是要获得本机的IP的话可以通过:

InetAddress.getLocalHost().toString()获得

 

 

public String getClientIP(HttpServletRequest request){
		String ip = request.getHeader("x-real-ip");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-forwarded-for");
			if(ip != null) {
				ip=ip.split(",")[0].trim();
			}
		}
		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;       
	}

 

根据HttpServletRequest取得ip

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号