首页  ·  知识 ·  编程语言
关于java的split点“.”IP限制类
blurxx  http://www.new321.com/blog/  Java  编辑:dezai  图片来源:网络
/**java split . 要用 [.]例如ip地址 或 包*/// IP限制类 源码public class IPUtils {<
/**
    java split . 要用 [.]
    例如ip地址 或 包
*/
// IP限制类 源码
public class IPUtils {

    public static int[] IPsplit(String ip) {
        return IPsplit(ip,false);
    }
    /**
     * reutrn a int array with 4 size
     *
     * @param ip
     * @return
     */
    public static int[] IPsplit(String ip,boolean max) {
        int[] inum = new int[4];
        try {
            String[] nums = ip.split("[.]");
            int len =nums.length;
            for (int i = 0; i < len; i++) {
                inum[i] = Integer.parseInt(nums[i]);
            }
            if(max) {//if max ,fill 256 to int[]
                if(len                    for (int j = len; j < inum.length; j++) {
                        inum[j]=256;
                    }
                }
            }
        } catch (Exception e) {
        }
        return inum;
    }

    public static boolean isBetween(String myIP, String ip) {
        return isBetween(myIP, ip, ip);
    }

    /**
     * return myIP is between ip1~ip2
     *
     * @param myIP
     * @param ip1
     * @param ip2
     * @return
     */
    public static boolean isBetween(String myIP, String ip1, String ip2) {
        if (myIP == null) {
            return true;
        }
        if (null == ip1) {
            ip1 = "0.0.0.0";
        }
        if (null == ip2) {
            ip2 = "256.0.0.0";
        }
        int[] myIPs = IPsplit(myIP);

        return arraySub(myIPs, IPsplit(ip2,true)) && arraySub(IPsplit(ip1), myIPs);
    }

    private static boolean arraySub(int[] a, int[] b) {
        for (int i = 0; i < a.length; i++) {
            if (b[i] - a[i] > 0) {
                return true;
            } else if (b[i] - a[i] < 0) {
                return false;
            } else {
                if(i+1==a.length) {//check is end
                    return true;
                }else {
                    continue;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        String myIP = "61.52.31.68";
        String ip1 = "61.52.31.68";
        String ip2 = "61.52.31.68";
        System.out.println(isBetween(myIP, ip1,ip2));

    }

} 本文作者:blurxx 来源:http://www.new321.com/blog/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读