最近在做IP数据来源的统计,需要统计来源的真实IP,于是就有了下文
- public class ObtainIP {
- /**
- * 获取访问请求的真实IP
- * @param args
- */
- 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.getRemoteAddr();
- }
- return ip;
- }
- }
通过request.getHeader("x-forwarded-for")可以获取到真实的IP地址。
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/717948,如需转载请自行联系原作者