java获取真实的客户端ip,用java后台获取ip地址

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/my773962804/article/details/82767723
// 获取客户端ip
public static String getClientIp(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();
   }
   if (ip == null || ip.length() == 0 || ip.indexOf(":") > -1) {
      try {
         ip = InetAddress.getLocalHost().getHostAddress();
      } catch (UnknownHostException e) {
         ip = null;
      }
   }
   return ip;
}

猜你喜欢

转载自blog.csdn.net/my773962804/article/details/82767723
今日推荐