`

Ip处理工具类

    博客分类:
  • Java
阅读更多
public class IpUtis {

    /**
     * 把long类型的Ip转为一般Ip类型:xx.xx.xx.xx
     * 
     * @param ip
     * @return
     */
    public static String getIpFromLong(Long ip) {
        String s1 = String.valueOf((ip & 4278190080L) / 16777216L);
        String s2 = String.valueOf((ip & 16711680L) / 65536L);
        String s3 = String.valueOf((ip & 65280L) / 256L);
        String s4 = String.valueOf(ip & 255L);
        return s1 + "." + s2 + "." + s3 + "." + s4;
    }

    /**
     * 把xx.xx.xx.xx类型的转为long类型的
     * 
     * @param ip
     * @return
     */
    public static Long getIpFromString(String ip) {
        Long ipLong = 0L;
        String ipTemp = ip;
        ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
        ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
        ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
        ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
        ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
        ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
        ipLong = ipLong * 256 + Long.parseLong(ipTemp);
        return ipLong;
    }

    /**
     * 根据掩码位获取掩码
     * 
     * @param maskBit 掩码位数,如"28"、"30"
     * @return
     */
    public static String getMaskByMaskBit(String maskBit) {
        return StringUtils.isEmpty(maskBit) ? "error, maskBit is null !" : maskBitMap().get(maskBit);
    }

    /**
     * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30
     * 
     * @param ip 给定的IP,如218.240.38.69
     * @param maskBit 给定的掩码位,如30
     * @return 起始IP的字符串表示
     */
    public static String getBeginIpStr(String ip, String maskBit) {
        return getIpFromLong(getBeginIpLong(ip, maskBit));
    }

    /**
     * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30
     * 
     * @param ip 给定的IP,如218.240.38.69
     * @param maskBit 给定的掩码位,如30
     * @return 起始IP的长整型表示
     */
    public static Long getBeginIpLong(String ip, String maskBit) {
        return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit));
    }

    /**
     * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30
     * 
     * @param ip 给定的IP,如218.240.38.69
     * @param maskBit 给定的掩码位,如30
     * @return 终止IP的字符串表示
     */
    public static String getEndIpStr(String ip, String maskBit) {
        return getIpFromLong(getEndIpLong(ip, maskBit));
    }

    /**
     * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30
     * 
     * @param ip 给定的IP,如218.240.38.69
     * @param maskBit 给定的掩码位,如30
     * @return 终止IP的长整型表示
     */
    public static Long getEndIpLong(String ip, String maskBit) {
        return getBeginIpLong(ip, maskBit) + ~getIpFromString(getMaskByMaskBit(maskBit));
    }

    /**
     * 根据子网掩码转换为掩码位 如 255.255.255.252转换为掩码位 为 30
     * 
     * @param netmarks
     * @return
     */
    public static int getNetMask(String netmarks) {
        StringBuffer sbf;
        String str;
        int inetmask = 0, count = 0;
        String[] ipList = netmarks.split("\\.");
        for (int n = 0; n < ipList.length; n++) {
            sbf = toBin(Integer.parseInt(ipList[n]));
            str = sbf.reverse().toString();
            count = 0;
            for (int i = 0; i < str.length(); i++) {
                i = str.indexOf('1', i);
                if (i == -1) {
                    break;
                }
                count++;
            }
            inetmask += count;
        }
        return inetmask;
    }

    /**
     * 计算子网大小
     * 
     * @param netmask 掩码位
     * @return
     */
    public static int getPoolMax(int maskBit) {
        if (maskBit <= 0 || maskBit >= 32) {
            return 0;
        }
        return (int) Math.pow(2, 32 - maskBit) - 2;
    }

    private static StringBuffer toBin(int x) {
        StringBuffer result = new StringBuffer();
        result.append(x % 2);
        x /= 2;
        while (x > 0) {
            result.append(x % 2);
            x /= 2;
        }
        return result;
    }

    /*
     * 存储着所有的掩码位及对应的掩码 key:掩码位 value:掩码(x.x.x.x)
     */
    private static Map<String, String> maskBitMap() {
        Map<String, String> maskBit = new HashMap<String, String>();
        maskBit.put("1", "128.0.0.0");
        maskBit.put("2", "192.0.0.0");
        maskBit.put("3", "224.0.0.0");
        maskBit.put("4", "240.0.0.0");
        maskBit.put("5", "248.0.0.0");
        maskBit.put("6", "252.0.0.0");
        maskBit.put("7", "254.0.0.0");
        maskBit.put("8", "255.0.0.0");
        maskBit.put("9", "255.128.0.0");
        maskBit.put("10", "255.192.0.0");
        maskBit.put("11", "255.224.0.0");
        maskBit.put("12", "255.240.0.0");
        maskBit.put("13", "255.248.0.0");
        maskBit.put("14", "255.252.0.0");
        maskBit.put("15", "255.254.0.0");
        maskBit.put("16", "255.255.0.0");
        maskBit.put("17", "255.255.128.0");
        maskBit.put("18", "255.255.192.0");
        maskBit.put("19", "255.255.224.0");
        maskBit.put("20", "255.255.240.0");
        maskBit.put("21", "255.255.248.0");
        maskBit.put("22", "255.255.252.0");
        maskBit.put("23", "255.255.254.0");
        maskBit.put("24", "255.255.255.0");
        maskBit.put("25", "255.255.255.128");
        maskBit.put("26", "255.255.255.192");
        maskBit.put("27", "255.255.255.224");
        maskBit.put("28", "255.255.255.240");
        maskBit.put("29", "255.255.255.248");
        maskBit.put("30", "255.255.255.252");
        maskBit.put("31", "255.255.255.254");
        maskBit.put("32", "255.255.255.255");
        return maskBit;
    }

    /**
     * 判断某个ip是否在一个网段内 ip/mask IP+掩码
     */
    public static boolean isInRange(String ip, String cidr) {
        String[] ips = ip.split("\\.");
        int ipAddr = (Integer.parseInt(ips[0]) << 24) | (Integer.parseInt(ips[1]) << 16)
                | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
        int type = Integer.parseInt(cidr.replaceAll(".*/", ""));
        int mask = 0xFFFFFFFF << (32 - type);
        String cidrIp = cidr.replaceAll("/.*", "");
        String[] cidrIps = cidrIp.split("\\.");
        int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | (Integer.parseInt(cidrIps[1]) << 16)
                | (Integer.parseInt(cidrIps[2]) << 8) | Integer.parseInt(cidrIps[3]);

        return (ipAddr & mask) == (cidrIpAddr & mask);
    }

    /**
     * 判断IP地址是否内网IP
     * 
     * @param ipAddress
     * @return
     */
    public static boolean isInnerIP(String ipAddress) {
        boolean isInnerIp = false;
        long ipNum = getIpNum(ipAddress);
        /**
         * 私有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这个网段是环回地址
         **/
        long aBegin = getIpNum("10.0.0.0");
        long aEnd = getIpNum("10.255.255.255");
        long bBegin = getIpNum("172.16.0.0");
        long bEnd = getIpNum("172.31.255.255");
        long cBegin = getIpNum("192.168.0.0");
        long cEnd = getIpNum("192.168.255.255");
        isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd)
                || ipAddress.equals("127.0.0.1");
        return isInnerIp;
    }

    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[2]);
        long d = Integer.parseInt(ip[3]);

        long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
        return ipNum;
    }

    private static boolean isInner(long userIp, long begin, long end) {
        return (userIp >= begin) && (userIp <= end);
    }

    /**
     * 将127.0.0.1 形式的IP地址转换成10进制整数
     * 
     * @param strIP
     * @return
     */
    public static long ipToTenLong(String strIP) {
        long[] ip = new long[4];
        // 先找到IP地址字符串中.的位置
        int position1 = strIP.indexOf(".");
        int position2 = strIP.indexOf(".", position1 + 1);
        int position3 = strIP.indexOf(".", position2 + 1);
        // 将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(strIP.substring(0, position1));
        ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(strIP.substring(position3 + 1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    public static String ipToTwoString(String strIP) {
        long[] ip = new long[4];
        // 先找到IP地址字符串中.的位置
        int position1 = strIP.indexOf(".");
        int position2 = strIP.indexOf(".", position1 + 1);
        int position3 = strIP.indexOf(".", position2 + 1);
        // 将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(strIP.substring(0, position1));
        ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(strIP.substring(position3 + 1));
        Long ten = (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
        return Long.toBinaryString(ten);
    }

    /**
     * 将10进制整数形式转换成127.0.0.1形式的IP地址
     * 
     * @param longIP
     * @return
     */
    public static String tenLongToIP(long longIP) {
        StringBuffer sb = new StringBuffer("");
        // 直接右移24位
        sb.append(String.valueOf(longIP >>> 24));
        sb.append(".");
        // 将高8位置0,然后右移16位
        sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16));
        sb.append(".");
        sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8));
        sb.append(".");
        sb.append(String.valueOf(longIP & 0x000000FF));
        return sb.toString();
    }

}

 

分享到:
评论

相关推荐

    IP检查的工具类

    这是一个检测IP信息的工具类,可用来过滤一些特殊流量,比如国外流量,港澳台的流量,有比较新的IP地址库,是2014-03-05更新。一个工具类,调用文件

    java常用工具类

    文件工具类,Http请求工具类,图片处理工具类。...mail工具类,Map工具类,MD5编码工具类,数字工具类,随机数工具类,反射工具类,字符串处理工具类,URL工具类,XML工具类,常用的数据验证工具类

    javaweb项目常用工具包

    Base64工具类-字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具...

    Java基础之java处理ip的工具类

    主要介绍了Java基础应用,使用java处理ip的工具类的相关资料,需要的朋友可以参考下

    IPV6Util.rar

    ipv6段或范围拆分和补全,支持xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/123 格式ipv6段或范围拆分和补全,ipv6段或范围拆分和补全

    Java整理的基础工具类项目

    Java整理的基础工具类项目 ...读写锁控制强制读取缓存同步 线程池管理类 配置文件初始化启动读取 Cookie工具类 JavaBean工具类 IP工具类 Json对象转换工具类 ...地理坐标处理WGS-84坐标转换成...LocaleDate 日期处理工具类

    Java 工具类 ping Ip 是否通

    通过参数 ip 判断网络是否连接通常,public static boolean getPing(String ip){ // 方法一 最常用的 PING 方法 Runtime runtime = Runtime.getRuntime(); // 获取当前程序的运行进对象 ... // 声明处理类对象

    C#200个基础工具类大全.zip

    IP辅助类;JSON操作;JS操作;URL的操作类;XML操作类;处理多媒体的公共类;弹出消息类;二维码操作类;汉字转拼音;加密解密;科学计数,数学;类型转换;配置文件操作类;上传下载;时间操作类;视频帮助类;数据展示控件绑定数据...

    网路岗7-IP报文分析工具

    “IP过滤”在捕包过滤使用最为常见,IP匹配主要分两类:一是不带通讯方向,单纯的是范围的匹配,如上图中的“From:to”类型;另外一类是带通讯方向的一对一匹配,如上图“”类型,不仅匹配IP地址,也匹配通讯的源IP...

    java开发常用工具类包.zip

    包含个人开发过程中常用工具类报,供大家方便提供,包含aes加密,格式化,excel导入导出,日期处理,IP获取,json转换等

    shawn-common-utils:Java整理的基础工具类项目

    读写锁控制强制读取缓存同步 线程池管理类 配置文件初始化启动读取 Cookie工具类 JavaBean工具类 IP工具类 Json对象转换工具类 MD5工具类 ...地理坐标处理WGS-84坐标转换成百度坐标工具类 ...LocaleDate 日期处理工具类

    TCP-IP技术大全

    IP网络中的名字和地址 29 4.1 IP寻址 29 4.1.1 二进制和十进制数 30 4.1.2 IPv4地址格式 30 4.2 子网的出现 34 4.2.1 分子网 35 4.2.2 可变长子网掩码(VLSM) 37 4.3 无类域前路由(CIDR) 38 ...

    TCP/IP技术大全

    24.5 IP迁移辅助工具 268 24.5.1 NDS 268 24.5.2 DNS 269 24.5.3 DHCP 269 24.5.4 DDNS 269 24.5.5 SLP 269 24.5.6 兼容模式 269 24.5.7 迁移代理 270 24.6 迁移策略 270 24.6.1 使用测试平台 270 24.6.2 迁移建议 ...

    IpTool抓包工具

    “IP过滤”在捕包过滤使用最为常见,IP匹配主要分两类:一是不带通讯方向,单纯的是范围的匹配,如上图中的“From:to”类型;另外一类是带通讯方向的一对一匹配,如上图“”类型,不仅匹配IP地址,也匹配通讯的源IP...

    TCP/IP教程TCP/IP基础

    第一部分 TCP/IP基础 第1章 开放式通信模型简介 1 1.1 开放式网络的发展 1 1.1.1 通信处理层次化 2 1.1.2 OSI参考模型 3 1.1.3 模型的使用 5 1.2 TCP/IP参考模型 7 1.3 小结 7 第2章 TCP/IP和Internet 8 2.1 一段...

    利用五类小工具自动化Windows故障处理命令

    在Windows系统里,微软提供了很多种命令行工具来帮助用户处理与正在运行的进程和TCP/IP连接问题相关的故障。但用户在选择使用这些工具的时间,就需要记下大量的参数设置信息,而这就意味着工作速度会下降很多。

Global site tag (gtag.js) - Google Analytics