//获取客户端ip
public static 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;
}
//获取本机所有ip(多网卡)
public static List<String> getLocalhostIp() {
List<String> ips = new ArrayList<String>();
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
InetAddress ia = null;
while (nis.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nis.nextElement();
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
ia = ias.nextElement();
if (ia instanceof Inet6Address)
continue;
if ("127.0.0.1".equals(ia.getHostAddress()))
continue;
ips.add(ia.getHostAddress());
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return ips;
}