欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java获取request请求客户端的IP地址

程序员文章站 2022-07-10 17:35:52
...

工具类如下:

public String getIpAddr(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.getRemoteAddr();     
     }     
     return ip;     
}    
相关标签: java获取ip