参考:https://blog.csdn.net/u013549582/article/details/81709453
/** * 获取ip地址 */ public static String getIpAddress() { httpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String ipAddress = request.getheader("x-forwarded-for"); if(ipAddress == null || ipAddress.length() == 0 || "unkNown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getheader("Proxy-ClIEnt-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unkNown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getheader("WL-Proxy-ClIEnt-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "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(); } ipAddress= inet.getHostAddress(); } } //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照‘,‘分割 if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15 if(ipAddress.indexOf(",")>0){ ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); } } return ipAddress; }
来自为知笔记(Wiz) 总结
以上是内存溢出为你收集整理的获取请求ip地址全部内容,希望文章能够帮你解决获取请求ip地址所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)