IP工具类

 在Java中,处理IP地址的工具类可以通过使用Java的内置库或者第三方库来实现。Java的java.net.InetAddress类提供了一些基本的IP地址处理功能,但是如果你需要更复杂的操作,比如IP地址范围检查、CIDR表示法处理等,那么使用第三方库可能更加方便。

public class IpUtils {
    /**
     * 日志信息
     */
    private static final Logger logger = LoggerFactory.getLogger(IpUtils.class);
    /**
     * ip的匹配规则
     */
    private static final String IP_REG = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";

    /**
     * 判断ip是否在局域网中
     * <p/>
     * 内网ip
     * A类 10.0.0.0 /8
     * B类 172.16.0.0--172.31.0.0   /16
     * C类 192.168.0.0--192.168.255.0 /24
     *
     * @param ip
     * @return
     */
    private IPBlock classA = new IPBlock("10.0.0.0", "255.0.0.0");

    private IPBlock classC = new IPBlock("192.168.0.0", "255.255.0.0");

    private boolean inLAN(String ip) {

        ip = ip.trim();

        int[] netIdSegment = divideToSegment(ip);
        if (netIdSegment == null) {
            logger.error("--errorIP--" + ip);
            return false;
        }

        if (classA.inMyBlock(netIdSegment)) {
            //a 类ip
            return true;
        }

        if (classC.inMyBlock(netIdSegment)) {
            //c类ip
            return true;
        }

        if (netIdSegment[0] == 172 && netIdSegment[1] >= 16 && netIdSegment[1] <= 31) {
            //b类ip
            return true;
        }

        if ("127.0.0.1".equals(ip)) {
            //本机地址
            return true;
        }

        return false;
    }


    private static String serverIP = null;

    public static String getServerIp() {

        if (serverIP == null) {
            try {
                Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
                InetAddress ip = null;
                while (allNetInterfaces.hasMoreElements()) {
                    NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                    if (netInterface.isLoopback()) {
                        continue;
                    }

                    Enumeration addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = (InetAddress) addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            serverIP = ip.getHostAddress().toString();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return serverIP;
    }

    /**
     * ip 点分十进制
     *
     * @param ip
     * @return
     */
    public static int[] divideToSegment(String ip) {

        if (!ip.matches(IP_REG)) {
            return null;
        }

        String[] segments = ip.split("\\.");
        int int_segments[] = new int[segments.length];
        for (int i = 0; i < segments.length; i++) {
            int_segments[i] = Integer.parseInt(segments[i]);
        }

        return int_segments;
    }

    static class IPBlock {

        private String subnetMask;        //子网掩码
        private String netId;     //网络号

        private int[] subnetMaskSegment;
        private int[] netIdSegment;


        public IPBlock(String netId, String subnetMask) {

            this.netId = netId;
            this.subnetMask = subnetMask;

            initSubnetMaskSegment(subnetMask);
            intiNetIdSegment(netId);
        }


        private void initSubnetMaskSegment(String ip) {
            this.subnetMaskSegment = IpUtils.divideToSegment(ip);
        }

        private void intiNetIdSegment(String ip) {
            this.netIdSegment = IpUtils.divideToSegment(ip);
        }

        public String getSubnetMask() {
            return subnetMask;
        }

        public void setSubnetMask(String subnetMask) {
            this.subnetMask = subnetMask;
        }

        public String getNetId() {
            return netId;
        }


        public int[] getSubnetMaskSegment() {
            return subnetMaskSegment;
        }


        public int[] getNetIdSegment() {
            return netIdSegment;
        }

        public boolean inMyBlock(String ip) {

            int segments[] = IpUtils.divideToSegment(ip);
            if (segments == null || segments.length < 4) {
                return false;
            }

            for (int i = 0; i < subnetMaskSegment.length; i++) {
                if ((segments[i] & subnetMaskSegment[i]) != netIdSegment[i]) {
                    return false;
                }
            }

            return true;
        }

        public boolean inMyBlock(int[] segments) {

            if (segments == null || segments.length < 4) {
                return false;
            }

            for (int i = 0; i < subnetMaskSegment.length; i++) {
                if ((segments[i] & subnetMaskSegment[i]) != netIdSegment[i]) {
                    return false;
                }
            }

            return true;
        }

    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进朱者赤

多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值