标签:
1 /** 2 * 获取当前网络ip 3 * @param request 4 * @return 5 */ 6 public String getIpAddr(HttpServletRequest request){ 7 String ipAddress = request.getHeader("x-forwarded-for"); 8 if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 9 ipAddress = request.getHeader("Proxy-Client-IP"); 10 } 11 if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 12 ipAddress = request.getHeader("WL-Proxy-Client-IP"); 13 } 14 if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 15 ipAddress = request.getRemoteAddr(); 16 if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){ 17 //根据网卡取本机配置的IP 18 InetAddress inet=null; 19 try { 20 inet = InetAddress.getLocalHost(); 21 } catch (UnknownHostException e) { 22 e.printStackTrace(); 23 } 24 ipAddress= inet.getHostAddress(); 25 } 26 } 27 //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照‘,‘分割 28 if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15 29 if(ipAddress.indexOf(",")>0){ 30 ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); 31 } 32 } 33 return ipAddress; 34 }
标签:
原文地址:http://www.cnblogs.com/codekey/p/4800537.html