Java获取请求地址Ip
public String getIpAddr(HttpServletRequest request) throws Exception {
String ipAddress = null;
String[] header = {"x-forwarded-for","Proxy-Client-IP","WL-Proxy-Client-IP"};
for (int i = 0; i < header.length; i++) {
ipAddress = request.getHeader(header[i]);
if (StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
continue;
} else {
break;
}
}
if (StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();