码迷,mamicode.com
首页 > Web开发 > 详细

JSP获取网络IP地址

时间:2015-02-12 09:14:32      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

import javax.servlet.http.HttpServletRequest;

public class RemoteAddress {

    public static String getRealClientIP(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        System.out.println(ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            System.out.println(ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            System.out.println(ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            System.out.println(ip);
        }

        return ip;
    }
}

之前用如上代码测试都发现结果是127.0.0.1,用本机做服务器挂网上中转也是。以为是代码出现问题,后来直接挂到空间上,发现正常显示出外网了,于是用移动网访问本机服务器也可以正常显示IP了而不再是127.0.0.1或者::1 的IPv6的地址。

JSP获取网络IP地址

标签:

原文地址:http://www.cnblogs.com/zempty/p/4287303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!