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

// 获取客户端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;
}

你可能感兴趣的:(JAVA小工具)