获取http请求的真实IP地址

来源:互联网 发布:c语言编程培训 编辑:程序博客网 时间:2024/05/19 19:57
/**     * 获取http请求的真实IP地址      * @param request     * @return     */    // cjianquan 2016/8/2    public static String getIPAddr(HttpServletRequest request){        if (request == null)            return null;        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 unknownhostexception) {            }        return ip;    }

1 0