用request对象获取客户端用户登入的IP地址,并将127.0.0.1和WIN7系统获取的0:0:0:0:0:0:0:1地址转换为实际的IP
public String getIpAddr(ServletRequestEvent servletRequestEvent) {
HttpServletRequest rquest = (HttpServletRequest) servletRequestEvent
.getServletRequest();
String ipAddress = null;
ipAddress = rquest.getHeader("x-forwarded-for");
if ((ipAddress == null) || (ipAddress.length() == 0)
|| ("unknown".equalsIgnoreCase(ipAddress))) {
ipAddress = rquest.getHeader("Proxy-Client-IP");
}
if ((ipAddress == null) || (ipAddress.length() == 0)
|| ("unknown".equalsIgnoreCase(ipAddress))) {
ipAddress = rquest.getHeader("WL-Proxy-Client-IP");
}
if ((ipAddress == null) || (ipAddress.length() == 0)
|| ("unknown".equalsIgnoreCase(ipAddress))) {
ipAddress = rquest.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")||ipAddress.equals("0:0:0:0:0:0:0:1")) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
if ((ipAddress != null) && (ipAddress.length() > 15)
&& (ipAddress.indexOf(",") > 0)) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
return ipAddress;
}
public String getIpAddr(ServletRequestEvent servletRequestEvent) {
HttpServletRequest rquest = (HttpServletRequest) servletRequestEvent
.getServletRequest();
String ipAddress = null;
ipAddress = rquest.getHeader("x-forwarded-for");
if ((ipAddress == null) || (ipAddress.length() == 0)
|| ("unknown".equalsIgnoreCase(ipAddress))) {
ipAddress = rquest.getHeader("Proxy-Client-IP");
}
if ((ipAddress == null) || (ipAddress.length() == 0)
|| ("unknown".equalsIgnoreCase(ipAddress))) {
ipAddress = rquest.getHeader("WL-Proxy-Client-IP");
}
if ((ipAddress == null) || (ipAddress.length() == 0)
|| ("unknown".equalsIgnoreCase(ipAddress))) {
ipAddress = rquest.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")||ipAddress.equals("0:0:0:0:0:0:0:1")) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
if ((ipAddress != null) && (ipAddress.length() > 15)
&& (ipAddress.indexOf(",") > 0)) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
return ipAddress;
}