Java根据IP地址进行分类排序

2022-05-16 10:37:23 浏览数 (2)

IP地址划分为 A,B,C,D,E五类

A类地址1.0.0.0~126.255.255.255;

B类地址128.0.0.0~191.255.255.255;

C类地址192.0.0.0~223.255.255.255;

D类地址224.0.0.0~239.255.255.255;

E类地址240.0.0.0~255.255.255.255

私网IP范围是:

10.0.0.0~10.255.255.255

172.16.0.0~172.31.255.255

192.168.0.0~192.168.255.255

代码语言:javascript复制
package com.hikvision.aradc.utils;
/**
 * @ProjectName IPUtils
 * @Author duanjiangcheng
 * @Description IP地址划分为 A,B,C,D,E五类
 * <p>
 * A类地址1.0.0.0~126.255.255.255;
 * <p>
 * B类地址128.0.0.0~191.255.255.255;
 * <p>
 * C类地址192.0.0.0~223.255.255.255;
 * <p>
 * D类地址224.0.0.0~239.255.255.255;
 * <p>
 * E类地址240.0.0.0~255.255.255.255
 * <p>
 * 私网IP范围是:
 * <p>
 * 10.0.0.0~10.255.255.255
 * <p>
 * 172.16.0.0~172.31.255.255
 * <p>
 * 192.168.0.0~192.168.255.255
 * @time 2022/5/16 9:51
 * @version 1.0
 */

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @ClassName IPUtils
 * @Author duanjiangcheng
 * @Description 根据IP进行分类,排序
 * @time 2022/5/16 9:51
 */
public class IPUtils {
    /*1代表A类,2代表B类,3代表C类;4代表其它类型*/


    /**
     * A类地址
     */
    public final static int IP_A_TYPE = 1;
    /**
     * B 类地址
     */
    public final static int IP_B_TYPE = 2;
    /**
     * C类地址
     */
    public final static int IP_C_TYPE = 3;
    /**
     * 其他类地址
     */
    public final static int IP_OTHER_TYPE = 4;

    /**
     * A类地址范围:1.0.0.1---126.255.255.254
     **/
    private static int[] IpATypeRange;

    /**
     * B类地址范围:128.0.0.1---191.255.255.254
     */
    private static int[] IpBTypeRange;

    /**
     * C类地址范围:192.168.0.0~192.168.255.255
     */
    private static int[] IpCTypeRange;

    /**
     * A,B,C类地址的默认mask
     */
    private static int DefaultIpAMask;
    private static int DefaultIpBMask;
    private static int DefaultIpCMask;

    // 初始化
    static {
        IpATypeRange = new int[2];
        IpATypeRange[0] = getIpV4Value("1.0.0.1");
        IpATypeRange[1] = getIpV4Value("126.255.255.254");

        IpBTypeRange = new int[2];
        IpBTypeRange[0] = getIpV4Value("128.0.0.1");
        IpBTypeRange[1] = getIpV4Value("191.255.255.254");

        IpCTypeRange = new int[2];
        IpCTypeRange[0] = getIpV4Value("192.168.0.0");
        IpCTypeRange[1] = getIpV4Value("192.168.255.255");

        DefaultIpAMask = getIpV4Value("255.0.0.0");
        DefaultIpBMask = getIpV4Value("255.255.0.0");
        DefaultIpCMask = getIpV4Value("255.255.255.0");
    }


    public static int getIpV4Value(String ipOrMask) {
        byte[] addr = getIpV4Bytes(ipOrMask);
        int address1 = addr[3] & 0xFF;
        address1 |= ((addr[2] << 8) & 0xFF00);
        address1 |= ((addr[1] << 16) & 0xFF0000);
        address1 |= ((addr[0] << 24) & 0xFF000000);
        return address1;
    }


    public static byte[] getIpV4Bytes(String ipOrMask) {
        try {

            String[] addrs = ipOrMask.split("\.");
            int length = addrs.length;
            byte[] addr = new byte[length];
            for (int index = 0; index < length; index  ) {
                addr[index] = (byte) (Integer.parseInt(addrs[index]) & 0xff);
            }
            return addr;
        } catch (Exception e) {
        }
        return new byte[4];
    }


    /**
     * 检测ipV4 的类型,包括A类,B类,C类,其它(C,D和广播)类等
     *
     * @param ipV4
     * @return 返回1代表A类,返回2代表B类,返回3代表C类;返回4代表D类
     */
    public static int checkIpV4Type(String ipV4) {
        int inValue = getIpV4Value(ipV4);
        if (inValue >= IpCTypeRange[0] && inValue <= IpCTypeRange[1]) {
            return IP_C_TYPE;
        } else if (inValue >= IpBTypeRange[0] && inValue <= IpBTypeRange[1]) {
            return IP_B_TYPE;
        } else if (inValue >= IpATypeRange[0] && inValue <= IpATypeRange[1]) {
            return IP_A_TYPE;
        }
        return IP_OTHER_TYPE;
    }


    public static List<String> listToSort(List<String> ips) {
        return ips.stream().sorted((e1, e2) -> {

            StringTokenizer token = new StringTokenizer(e1, ".");
            StringTokenizer token2 = new StringTokenizer(e2, ".");
            while (token.hasMoreTokens() && token2.hasMoreTokens()) {
                int parseInt = Integer.parseInt(token.nextToken());
                int parseInt2 = Integer.parseInt(token2.nextToken());
                if (parseInt > parseInt2) {
                    return 1;
                }
                if (parseInt < parseInt2) {
                    return -1;
                }

            }
            if (token.hasMoreElements()) { // e1还有值,则e2已遍历完
                return 1;
            } else {
                return -1;
            }
        }).collect(Collectors.toList());
    }

    public static void addAll(List<List<String>> list, List<String> type) {
        if (!CollectionUtils.isEmpty(type)) {
            list.add(type);
        }
    }

    public static List<List<String>> listToGroup(List<String> ips) {
        List<List<String>> list = new ArrayList<>(4);
        List<String> ip_a_type_list = ips.stream().filter(item -> checkIpV4Type(item) == IP_A_TYPE).collect(Collectors.toList());
        List<String> ip_b_type_list = ips.stream().filter(item -> checkIpV4Type(item) == IP_B_TYPE).collect(Collectors.toList());
        List<String> ip_c_type_list = ips.stream().filter(item -> checkIpV4Type(item) == IP_C_TYPE).collect(Collectors.toList());
        List<String> ip_other_type_list = ips.stream().filter(item -> checkIpV4Type(item) == IP_OTHER_TYPE).collect(Collectors.toList());
        addAll(list, listToSort(ip_a_type_list));
        addAll(list, listToSort(ip_b_type_list));
        addAll(list, listToSort(ip_c_type_list));
        addAll(list, listToSort(ip_other_type_list));
        return list;
    }


    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("10.9.96.200");
        list.add("10.9.96.199");
        list.add("10.9.96.202");
        list.add("192.168.0.158");
        System.out.println(listToGroup(list));
    }

}
代码语言:javascript复制
输入的结果为
[[10.9.96.199, 10.9.96.200, 10.9.96.202], [192.168.0.158]]

0 人点赞