Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165173
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-06-29 16:11:09


包含:
  • 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
  • 获取本地IP地址
  • 判断操作系统是否是Windows

点击(此处)折叠或打开

  1. import lombok.extern.log4j.Log4j;

  2. import javax.servlet.http.HttpServletRequest;
  3. import java.net.InetAddress;
  4. import java.net.NetworkInterface;
  5. import java.net.SocketException;
  6. import java.net.UnknownHostException;
  7. import java.util.Enumeration;

  8. /**
  9.  * Created by wyh on 16/12/19.
  10.  */
  11. @Log4j
  12. public final class NetworkUtil {

  13.     /**
  14.      * 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
  15.      *
  16.      * @param request
  17.      * @return
  18.      * @throws
  19.      */
  20.     public final static String getIpAddress(HttpServletRequest request) {
  21.         // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址

  22.         String ip = request.getHeader("X-Forwarded-For");
  23.         if (log.isInfoEnabled()) {
  24.             log.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
  25.         }

  26.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  27.             if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  28.                 ip = request.getHeader("Proxy-Client-IP");
  29.                 if (log.isInfoEnabled()) {
  30.                     log.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
  31.                 }
  32.             }
  33.             if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  34.                 ip = request.getHeader("WL-Proxy-Client-IP");
  35.                 if (log.isInfoEnabled()) {
  36.                     log.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
  37.                 }
  38.             }
  39.             if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  40.                 ip = request.getHeader("HTTP_CLIENT_IP");
  41.                 if (log.isInfoEnabled()) {
  42.                     log.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
  43.                 }
  44.             }
  45.             if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  46.                 ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  47.                 if (log.isInfoEnabled()) {
  48.                     log.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
  49.                 }
  50.             }
  51.             if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  52.                 ip = request.getRemoteAddr();
  53.                 if (log.isInfoEnabled()) {
  54.                     log.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
  55.                 }
  56.             }
  57.         } else if (ip.length() > 15) {
  58.             String[] ips = ip.split(",");
  59.             for (int index = 0; index < ips.length; index++) {
  60.                 String strIp = (String) ips[index];
  61.                 if (!("unknown".equalsIgnoreCase(strIp))) {
  62.                     ip = strIp;
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.         return ip;
  68.     }



  69.     /**
  70.      * 获取本地IP地址
  71.      *
  72.      * @throws SocketException
  73.      */
  74.     public static String getLocalIP() {
  75.         try {
  76.             if (isWindowsOS()) {
  77.                 return InetAddress.getLocalHost().getHostAddress();
  78.             } else {
  79.                 return getLinuxLocalIp();
  80.             }
  81.         } catch (UnknownHostException e) {
  82.             e.printStackTrace();
  83.             log.error("获取ip地址异常", e);
  84.         }
  85.         return "";
  86.     }

  87.     /**
  88.      * 获取Linux下的IP地址
  89.      *
  90.      * @return IP地址
  91.      * @throws SocketException
  92.      */
  93.     private static String getLinuxLocalIp() {
  94.         String ip = "";
  95.         try {
  96.             for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
  97.                 NetworkInterface intf = en.nextElement();
  98.                 String name = intf.getName();
  99.                 if (!name.contains("docker") && !name.contains("lo")) {
  100.                     for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
  101.                         InetAddress inetAddress = enumIpAddr.nextElement();
  102.                         if (!inetAddress.isLoopbackAddress()) {
  103.                             String ipaddress = inetAddress.getHostAddress().toString();
  104.                             if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
  105.                                 ip = ipaddress;
  106.                             }
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.         } catch (SocketException ex) {
  112.             log.error("获取ip地址异常", ex);
  113.             ip = "127.0.0.1";
  114.             ex.printStackTrace();
  115.         }
  116.         return ip;
  117.     }

  118.     /**
  119.      * 判断操作系统是否是Windows
  120.      *
  121.      * @return
  122.      */
  123.     public static boolean isWindowsOS() {
  124.         boolean isWindowsOS = false;
  125.         String osName = System.getProperty("os.name");
  126.         if (osName.toLowerCase().indexOf("windows") > -1) {
  127.             isWindowsOS = true;
  128.         }
  129.         return isWindowsOS;
  130.     }

  131.     /**
  132.      * 获取本地Host名称
  133.      */
  134.     public static String getLocalHostName() throws UnknownHostException {
  135.         return InetAddress.getLocalHost().getHostName();
  136.     }

  137.     public static boolean isIpv4(String ipv4){
  138.         if(ipv4==null || ipv4.length()==0){
  139.             return false;//字符串为空或者空串
  140.         }
  141.         String[] parts=ipv4.split("\\.");//因为java doc里已经说明, split的参数是reg, 即正则表达式, 如果用"|"分割, 则需使用"\\|"
  142.         if(parts.length!=4){
  143.             return false;//分割开的数组根本就不是4个数字
  144.         }
  145.         for(int i=0;i<parts.length;i++){
  146.             try{
  147.                 int n=Integer.parseInt(parts[i]);
  148.                 if(n<0 || n>255){
  149.                     return false;//数字不在正确范围内
  150.                 }
  151.             }catch (NumberFormatException e) {
  152.                 return false;//转换数字不正确
  153.             }
  154.         }
  155.         return true;
  156.     }

  157. }


阅读(1937) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~