java获得访问者的ip代码

如果有不对和不全的情况请指出,方便改正。
package com.util;

import javax.servlet.http.HttpServletRequest;

public class GetIPAddr {
public String getIP(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();
   }
   String[] arg=ip.split(",");
   return arg[0];
   }
}

你可能感兴趣的:(java,servlet)