java获取访问地址IP的简单方法

//获取外网IP
public String getIpAddr(HttpServletRequest request) {
//处理代理访问获取不到真正的ip问题的
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//获取代理中中的ip
ip = request.getHeader("PRoxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//获取代理中中的ip

ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//非代理的情况获取ip
ip = request.getRemoteAddr();
}

return ip;
}

上一篇:2021.6.28-2021.7.2


下一篇:wenjien