zoukankan      html  css  js  c++  java
  • JAVA判断IP是否是内网IP

    /**
         * 私有IP:
         * A类  10.0.0.0-10.255.255.255  
         * B类  172.16.0.0-172.31.255.255  
         * C类  192.168.0.0-192.168.255.255           
         *
         * 127这个网段是环回地址
         * localhost  
         */
        static List<Pattern> ipFilterRegexList = new ArrayList<>();
    
        static {
            Set<String> ipFilter = new HashSet<String>();
            ipFilter.add("^10\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])"
                    + "\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])" + "\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$");
            // B类地址范围: 172.16.0.0---172.31.255.255
            ipFilter.add("^172\.(1[6789]|2[0-9]|3[01])\" + ".(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\"
                    + ".(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$");
            // C类地址范围: 192.168.0.0---192.168.255.255
            ipFilter.add("^192\.168\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\"
                    + ".(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$");
            ipFilter.add("127.0.0.1");
            ipFilter.add("0.0.0.0");
            ipFilter.add("localhost");
            for (String tmp : ipFilter) {
                ipFilterRegexList.add(Pattern.compile(tmp));
            }
        }
    
    
    
    /**
         * 判断IP是否内网IP
         * @Title: ipIsInner
         * @param ip
         * @return: boolean
         */
        public static boolean ipIsInner(String ip) {
            boolean isInnerIp = false;
            for (Pattern tmp : ipFilterRegexList) {
                Matcher matcher = tmp.matcher(ip);
                if (matcher.find()) {
                    isInnerIp = true;
                    break;
                }
            }
            return isInnerIp;
        }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    Aster寻路算法1(转)
    谈谈项目纵向项目验收
    要有兴趣
    用c# 操作 文件的方法
    使用ACT进行测试
    Generated servlet error: keyword cannot be resolved or is not a type
    米尔顿艾瑞克森的催眠引导词
    c# webcliend 来制作 网页搜捕器
    网页木马的解决方案
    用Swing实现数据表格功能
  • 原文地址:https://www.cnblogs.com/pxblog/p/13740637.html
Copyright © 2011-2022 走看看