java获取用户ip

    /**
     * @title 获取访问用户的真实IP地址
     * @param request
     * @return ip
     */
    public static String getIpAddr(HttpServletRequest request)
    {
        String addr = request.getHeader("x-forwarded-for");
        if (addr == null || addr.length() == 0 || "unknown".equalsIgnoreCase(addr))
        {
            addr = request.getHeader("Proxy-Client-IP");
        }
        if (addr == null || addr.length() == 0 || "unknown".equalsIgnoreCase(addr))
        {
            addr = request.getHeader("WL-Proxy-Client-IP");
        }
        if (addr == null || addr.length() == 0 || "unknown".equalsIgnoreCase(addr))
        {
            addr = request.getRemoteAddr();
        }
        return addr;
    }

posted @ 2017-06-22 16:44  walle1314  阅读(181)  评论(0编辑  收藏  举报