您的位置:

Java获取请求IP的方法

一、使用HttpServletRequest对象获取IP地址

Java Web中可以使用HttpServletRequest对象来获取客户端的IP地址。使用以下代码可以获取IP地址:

public String getIpAddress(HttpServletRequest request) {
    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();
    }
    return ip;
}

以上代码从request的头部信息中获取IP地址,如果没有则返回request的远程地址。

二、使用Java Web容器的方法获取IP地址

有些Java Web容器(如Tomcat)提供了获取IP的方法,可以直接调用该方法来获取IP地址。以Tomcat为例,可以使用下面的代码获取IP地址:

public String getIpAddress(HttpServletRequest request) {
    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();
    }
    return ip;
}

以上代码从request的头部信息中获取IP地址,如果没有则返回request的远程地址。

三、使用第三方库获取IP地址

除了使用HttpServletRequest对象和Java Web容器提供的获取IP地址的方法外,还可以使用第三方库来获取IP地址。

常用的第三方库包括:

  • Apache Commons工具包
  • Google Guava工具包
  • Spring Framework工具包

以下是使用Apache Commons工具包获取IP地址的代码:

public String getIpAddress(HttpServletRequest request) {
    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 (ip != null) {
        ip = org.apache.commons.lang.StringUtils.stripToNull(ip.split(",")[0]);
    }
    return ip;
}

以上代码使用了Apache Commons工具包中的StringUtils类来处理获取到的IP地址。

四、总结

Java可以通过HttpServletRequest对象、Java Web容器提供的方法、第三方库来获取请求IP地址。其中,HttpServletRequest对象是常用的获取IP地址的方法,同时Apache Commons工具包也是很常用的第三方库。