首页 > 其他 > 详细

IP 地址获取

时间:2015-07-17 23:06:21      阅读:345      评论:0      收藏:0      [点我收藏+]

public String getIPAddr() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isNullOrEmpty(ip)|| "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");  
        }
        if (StringUtils.isNullOrEmpty(ip)|| "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isNullOrEmpty(ip)|| "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isNullOrEmpty(ip)|| "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isNullOrEmpty(ip)|| "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isNullOrEmpty(ip)|| "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if(!StringUtils.isNullOrEmpty(ip)&&ip.indexOf(",")!=-1){
            ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
        }
        logger.info("x-forwarded-for:"+request.getHeader("x-forwarded-for"));
        logger.info("Proxy-Client-IP:"+request.getHeader("Proxy-Client-IP"));
        logger.info("WL-Proxy-Client-IP:"+request.getHeader("WL-Proxy-Client-IP"));
        logger.info("HTTP_CLIENT_IP:"+request.getHeader("HTTP_CLIENT_IP"));
        logger.info("HTTP_X_FORWARDED_FOR:"+request.getHeader("HTTP_X_FORWARDED_FOR"));
        logger.info("http_client_ip:"+request.getHeader("http_client_ip"));
        logger.info("remote_ip:"+request.getRemoteAddr());
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }


public static boolean isNullOrEmpty(String str) {
        return str == null || "".equals(str) || "null".equals(str);
    }


isNullOrEmpty方法为自己写的,稍微修改下

IP 地址获取

原文:http://tianjian.blog.51cto.com/3549910/1675660

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!