Java 获取Ip 方法

// 对方发起请求时,将HttpServletRequest 传入,将请求request取出IP

public String getIPByRequest(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    if (StringUtils.isEmpty(ip)) {
        ip = "not-get-ip-yet";
    }
    return ip;
}

 

你可能感兴趣的:(Java 获取Ip 方法)