JAVA获取当前IP地址

{
    String ip = null;
    try {
        ip = request.getHeader("x-forwarded-for");
        if (org.apache.commons.lang3.StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        //        //使用代理,则获取第一个IP地址
        if ((!org.apache.commons.lang3.StringUtils.isEmpty(ip)) && ip.length() > 8) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }

    } catch (Exception e) {
        logger.error("getIpAddr ERROR ", e);
    }

    return ip;
}

你可能感兴趣的:(技术简介,java)