• IP网段拆分出多个IP


    package com.lzk.test01.test;
    
    import lombok.extern.slf4j.Slf4j;
    
    import java.io.*;
    
    @Slf4j
    public class IpTest1 {
        public static void main(String[] args) throws Exception {
            String demo = "1.0.1.5-2.0.2.4";
            String[] split = demo.split("-");
            String ip1 = split[0];
            String ip2 = split[1];
            //todo 校验ip格式
            if (ip1.equals(ip2)) {
                System.out.println("ip网段不能相同");
            }
            String[] split1 = ip1.split("\.");
            String[] split2 = ip2.split("\.");
            int aNum = 0, bNum = 0, cNum = 0, dNum = 0;
            boolean aBoolean = split1[0].equals(split2[0]);
            boolean bBoolean = split1[1].equals(split2[1]);
            boolean cBoolean = split1[2].equals(split2[2]);
            boolean dBoolean = split1[3].equals(split2[3]);
            int a1 = Integer.parseInt(split1[0]);
            int a2 = Integer.parseInt(split2[0]);
            int b1 = Integer.parseInt(split1[1]);
            int b2 = Integer.parseInt(split2[1]);
            int c1 = Integer.parseInt(split1[2]);
            int c2 = Integer.parseInt(split2[2]);
            int d1 = Integer.parseInt(split1[3]);
            int d2 = Integer.parseInt(split2[3]);
            if (!aBoolean) {
                if (a1 > a2) {
                    throw new Exception("ip网段必须从小到大");
                }
                aNum = a2 - a1 + 1;
            }
            if (!bBoolean && aBoolean) {
                if (b1 > b2) {
                    throw new Exception("ip网段必须从小到大");
                }
                bNum = b2 - b1 + 1;
            }
            if(aNum > 0){
                bNum = 256;
            }
            if (!cBoolean && aBoolean && bBoolean) {
                if (c1 > c2) {
                    throw new Exception("ip网段必须从小到大");
                }
                cNum = c2 - c1 + 1;
            }
            if (!dBoolean && aBoolean && bBoolean && cBoolean) {
                if (d1 > d2) {
                    throw new Exception("ip网段必须从小到大");
                }
            }
            if(bNum > 0){
                cNum = 256;
            }
            if (aNum == 0) {
                aNum = 1;
            }
            if (bNum == 0) {
                bNum = 1;
            }
            if (cNum == 0) {
                cNum = 1;
            }
            try {
                File file = new File("ip.txt");
                file.createNewFile();
                FileWriter fileWriter = new FileWriter(file);
                BufferedWriter out = new BufferedWriter(fileWriter);
                out.write(ip1 + "
    ");
                boolean flag = false;
                for (int n = 0; n < aNum; n++) {
                    if(flag){
                        break;
                    }
                    for (int i = 0; i < bNum; i++) {
                        if(flag){
                            break;
                        }
                        for (int j = 0; j < cNum; j++) {
                            if(flag){
                                break;
                            }
                            for (int k = 0; k < 256; k++) {
                                d1++;
                                if (d1 == 256) {
                                    c1 ++;
                                    d1 = 0;
                                }
                                if (c1 == 256) {
                                    b1 ++;
                                    c1 = 0;
                                }
                                if (b1 == 256) {
                                    a1 ++;
                                    b1 = 0;
                                }
                                if (a1 > 255) {
                                    a1 = 255;
                                }
                                String s = a1 + "." + b1 + "." + c1 + "." + d1;
                                if (s.equals(ip2)) {
                                    log.info(a1 + "." + b1 + "." + c1 + "." + d1);
                                    out.write(a1 + "." + b1 + "." + c1 + "." + d1 + "
    ");
                                    flag = true;
                                    break;
                                }
                                log.info(a1 + "." + b1 + "." + c1 + "." + d1);
                                out.write(a1 + "." + b1 + "." + c1 + "." + d1 + "
    ");
                            }
                        }
                    }
                }
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("结束");
        }
    }
  • 相关阅读:
    STS IDE 个性化修改
    tomcat发布web项目,支持域名
    执行 maven 命令 报错Unable to add module to the current project as it is not of packaging type 'pom'[转]
    从数组中返回最大长度的所有子数组
    springboot 1.5.x 使用tomcat8设置cookie的domain以dot开头报错
    tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
    通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明
    Java原理之HashMap
    你应该知道的JAVA面试题
    sql 置顶功能的查询
  • 原文地址:https://www.cnblogs.com/liuzhengkun/p/14920032.html
Copyright © 2020-2023  润新知