IPv4地址段、地址掩码、可用地址等常用方法
package com.xxx.iptools; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class IPv4Util { public static void main(String[] args){ String ipAddr="15.10.44.60"; System.out.println("判断字符串是否为IPv4地址:"+isIPv4("24")); System.out.println("IPv4地址转换为数字:"+ipToInt(ipAddr)); System.out.println("数字转换为IP地址:"+intToIP(252324924)); System.out.println("数字串转换为IP地址:"+intToIP("252324928")); System.out.println("将掩码位转换为地址:"+maskToIP("8")); System.out.println("将掩码转换为掩码位:"+maskToBit("255.0.0.0")); System.out.println("根据IP和掩码计算首地址:"+IPmap("192.168.1.2/24")); System.out.println("根据IP和掩码计算所有IP地址:"+IPAll("192.168.1.15/28")); System.out.println("根据IP和掩码计算所有IP十进制值:"+IPnums("192.168.1.15/32")); System.out.println("两组IP判断包含(bIsContA:B包含A;aIsContB:A包含B)、交集(retainAll)、并集(all)、去重并集(disAll)、差集(diffAll,A有B没有):"+IPColl("192.168.1.1/30","192.168.1.1/31")); } /** * 根据IP地址和掩码,获取全量IP列表 * @param ipAddra,ipAddrb * @return map */ public static Map IPColl (String ipAddra,String ipAddrb){ //根据地址信息获取全量十进制IP List lista=IPnums(ipAddra); List listb=IPnums(ipAddrb); Map map=new HashMap(); List list = new ArrayList(); //包含关系判断 Boolean aIsContB=lista.containsAll(listb); map.put("aIsContB",aIsContB); Boolean bIsContA=listb.containsAll(lista); map.put("bIsContA",bIsContA); //并集,包含重复数据 list = lista; list.addAll(listb); map.put("all",listNuToIP(list)); list=null; //差集,lista对listb的差集,a有b没有的意思 list=lista; list.removeAll(listb); map.put("diffAll",listNuToIP(list)); //去重并集:根据上一步的差集,合并第二集合,得到去重并集 list.addAll(listb); map.put("disAll",listNuToIP(list)); list=null; //交集:两个list集合的交集 list=lista; list.retainAll(listb); map.put("retainAll",listNuToIP(list)); list=null; return map; } /** * 根据十进制IP list列表,获取点分制IP地址 * @param listint * @return list */ public static List listNuToIP (List listint){ List list=new ArrayList(); for (Object ip:listint){ String ips = intToIP(Long.parseLong(ip.toString())); list.add(ips); } return list; } /** * 根据IP地址和掩码,获取全量IP列表 * @param ipAddr * @return list */ public static List IPnums (String ipAddr){ List list=new ArrayList(); if (!ipAddr.contains("/")){ list.add(ipToInt(ipAddr)); return list; } if (ipAddr.split("/")[1].equals("32") || ipAddr.split("/")[1].equals("255.255.255.255")){ list.add(ipToInt(ipAddr.split("/")[0])); return list; } Map mapIP=IPmap(ipAddr); Long startIP=ipToInt(mapIP.get("netIP").toString()); Long endIP=ipToInt(mapIP.get("broadIP").toString()); for (Long i=startIP;i<=endIP;i++){ list.add(i); } return list; } /** * 根据IP地址和掩码,获取全量IP列表 * @param ipAddr * @return list */ public static List IPAll (String ipAddr){ List list=new ArrayList(); if (!ipAddr.contains("/")){ list.add(ipAddr); return list; } if (ipAddr.split("/")[1].equals("32") || ipAddr.split("/")[1].equals("255.255.255.255")){ list.add(ipAddr.split("/")[0]); return list; } Map mapIP=IPmap(ipAddr); Long startIP=ipToInt(mapIP.get("netIP").toString()); Long endIP=ipToInt(mapIP.get("broadIP").toString()); for (Long i=startIP;i<=endIP;i++){ list.add(intToIP(i)); } return list; } /** * 根据IP地址和掩码计算掩码、网络、广播、起始IP地址 * @param ipAddr * @return map */ public static Map<String,String> IPmap(String ipAddr){ Map<String,String> map=new HashMap<>(); if (!ipAddr.contains("/")){ map.put("startIP",ipAddr); map.put("maskBit","32"); map.put("maskIP","255.255.255.255"); return map; } String ip=ipAddr.split("/")[0]; String maskIP=ipAddr.split("/")[1]; String maskBit=""; if(isIPv4(maskIP)){ maskBit=maskToBit(maskIP); if (maskBit.equals("32")){ map.put("startIP",ip); map.put("maskBit","32"); map.put("maskIP","255.255.255.255"); return map; } }else{ maskBit=maskIP; if (maskBit.equals("32")){ map.put("startIP",ip); return map; } maskIP=maskToIP(maskBit); } String ip1st = ""; int ipArray[] = new int[4]; int netMaskArray[] = new int[4]; for (int i = 0; i <4; i++) { ipArray[i] = Integer.parseInt(ip.split("\\.")[i]); netMaskArray[i] = Integer.parseInt(maskIP.split("\\.")[i]); ipArray[i] = ipArray[i]&netMaskArray[i]; } for (int i = 0; i < 4; i++){ if(i == 3){ ipArray[i] = ipArray[i]; } if ("" == ip1st){ ip1st +=ipArray[i]; } else{ ip1st += "." + ipArray[i]; } } map.put("maskBit",maskBit); map.put("maskIP",maskIP); map.put("netIP",ip1st); String broadIP=intToIP(ipToInt(ip1st)+(int)Math.pow(2,32-Integer.parseInt(maskBit))-1); String end=intToIP(ipToInt(ip1st)+(int)Math.pow(2,32-Integer.parseInt(maskBit))-2); map.put("broadIP",broadIP); map.put("endIP",broadIP); ip1st=""; for (int i = 0; i < 4; i++){ if(i == 3){ ipArray[i] = ipArray[i]+1; } if ("" == ip1st){ ip1st +=ipArray[i]; } else{ ip1st += "." + ipArray[i]; } } map.put("startIP",ip1st); return map; } /** * 将掩码位转换为地址,入参为字符串 * @param maskIP * @return String */ public static String maskToBit(String maskIP) { String bitMask="-1"; switch (maskIP){ case "128.0.0.0" : bitMask = "1" ; break; case "192.0.0.0" : bitMask = "2" ; break; case "224.0.0.0" : bitMask = "3" ; break; case "240.0.0.0" : bitMask = "4" ; break; case "248.0.0.0" : bitMask = "5" ; break; case "252.0.0.0" : bitMask = "6" ; break; case "254.0.0.0" : bitMask = "7" ; break; case "255.0.0.0" : bitMask = "8" ; break; case "255.128.0.0" : bitMask = "9" ; break; case "255.192.0.0" : bitMask ="10" ; break; case "255.224.0.0" : bitMask ="11" ; break; case "255.240.0.0" : bitMask ="12" ; break; case "255.248.0.0" : bitMask ="13" ; break; case "255.252.0.0" : bitMask ="14" ; break; case "255.254.0.0" : bitMask ="15" ; break; case "255.255.0.0" : bitMask ="16" ; break; case "255.255.128.0" : bitMask ="17" ; break; case "255.255.192.0" : bitMask ="18" ; break; case "255.255.224.0" : bitMask ="19" ; break; case "255.255.240.0" : bitMask ="20" ; break; case "255.255.248.0" : bitMask ="21" ; break; case "255.255.252.0" : bitMask ="22" ; break; case "255.255.254.0" : bitMask ="23" ; break; case "255.255.255.0" : bitMask ="24" ; break; case "255.255.255.128" : bitMask ="25" ; break; case "255.255.255.192" : bitMask ="26" ; break; case "255.255.255.224" : bitMask ="27" ; break; case "255.255.255.240" : bitMask ="28" ; break; case "255.255.255.248" : bitMask ="29" ; break; case "255.255.255.252" : bitMask ="30" ; break; case "255.255.255.254" : bitMask ="31" ; break; case "255.255.255.255" : bitMask ="32" ; break; } return bitMask; } /** * 将掩码位转换为地址,入参为字符串 * @param maskBit * @return String */ public static String maskToIP(String maskBit) { String ipMask="-1"; switch (maskBit){ case "1" : ipMask = "128.0.0.0"; break; case "2" : ipMask = "192.0.0.0"; break; case "3" : ipMask = "224.0.0.0"; break; case "4" : ipMask = "240.0.0.0"; break; case "5" : ipMask = "248.0.0.0"; break; case "6" : ipMask = "252.0.0.0"; break; case "7" : ipMask = "254.0.0.0"; break; case "8" : ipMask = "255.0.0.0"; break; case "9" : ipMask = "255.128.0.0"; break; case "10" : ipMask = "255.192.0.0"; break; case "11" : ipMask = "255.224.0.0"; break; case "12" : ipMask = "255.240.0.0"; break; case "13" : ipMask = "255.248.0.0"; break; case "14" : ipMask = "255.252.0.0"; break; case "15" : ipMask = "255.254.0.0"; break; case "16" : ipMask = "255.255.0.0"; break; case "17" : ipMask = "255.255.128.0"; break; case "18" : ipMask = "255.255.192.0"; break; case "19" : ipMask = "255.255.224.0"; break; case "20" : ipMask = "255.255.240.0"; break; case "21" : ipMask = "255.255.248.0"; break; case "22" : ipMask = "255.255.252.0"; break; case "23" : ipMask = "255.255.254.0"; break; case "24" : ipMask = "255.255.255.0"; break; case "25" : ipMask = "255.255.255.128"; break; case "26" : ipMask = "255.255.255.192"; break; case "27" : ipMask = "255.255.255.224"; break; case "28" : ipMask = "255.255.255.240"; break; case "29" : ipMask = "255.255.255.248"; break; case "30" : ipMask = "255.255.255.252"; break; case "31" : ipMask = "255.255.255.254"; break; case "32" : ipMask = "255.255.255.255"; break; } return ipMask; } /** * 将掩码位转换为地址,入参为数字 * @param maskBit * @return String */ public static String maskToIP(int maskBit) { String ipMask="-1"; switch (maskBit){ case 1 : ipMask = "128.0.0.0"; break; case 2 : ipMask = "192.0.0.0"; break; case 3 : ipMask = "224.0.0.0"; break; case 4 : ipMask = "240.0.0.0"; break; case 5 : ipMask = "248.0.0.0"; break; case 6 : ipMask = "252.0.0.0"; break; case 7 : ipMask = "254.0.0.0"; break; case 8 : ipMask = "255.0.0.0"; break; case 9 : ipMask = "255.128.0.0"; break; case 10 : ipMask = "255.192.0.0"; break; case 11 : ipMask = "255.224.0.0"; break; case 12 : ipMask = "255.240.0.0"; break; case 13 : ipMask = "255.248.0.0"; break; case 14 : ipMask = "255.252.0.0"; break; case 15 : ipMask = "255.254.0.0"; break; case 16 : ipMask = "255.255.0.0"; break; case 17 : ipMask = "255.255.128.0"; break; case 18 : ipMask = "255.255.192.0"; break; case 19 : ipMask = "255.255.224.0"; break; case 20 : ipMask = "255.255.240.0"; break; case 21 : ipMask = "255.255.248.0"; break; case 22 : ipMask = "255.255.252.0"; break; case 23 : ipMask = "255.255.254.0"; break; case 24 : ipMask = "255.255.255.0"; break; case 25 : ipMask = "255.255.255.128"; break; case 26 : ipMask = "255.255.255.192"; break; case 27 : ipMask = "255.255.255.224"; break; case 28 : ipMask = "255.255.255.240"; break; case 29 : ipMask = "255.255.255.248"; break; case 30 : ipMask = "255.255.255.252"; break; case 31 : ipMask = "255.255.255.254"; break; case 32 : ipMask = "255.255.255.255"; break; } return ipMask; } /** * 将数字转换为IPv4地址 * @param intIPv4 * @return String */ public static String intToIP(long intIPv4) { return ((intIPv4 >> 24) & 0xFF) + "." + ((intIPv4 >> 16) & 0xFF) + "." + ((intIPv4 >> 8) & 0xFF) + "." + (intIPv4 & 0xFF); } /** * 将数字转换为IPv4地址 * @param intIPv4 * @return String */ public static String intToIP(String intIPv4) { Long ip=Long.parseLong(intIPv4); return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF); } /** * 将IPv4地址转换为数字 * @param ipAddr * @return long */ public static Long ipToInt(String ipAddr){ long ipInt = 0; String[] ipAddrArray = ipAddr.split("\\."); for (int i = 3;i>=0;i--){ long ip=Long.parseLong(ipAddrArray[3-i]); ipInt |=ip<<(i*8); } return ipInt; } /** * 判断IPv4地址是否合法 * @param ipAddr * @return boolean */ public static boolean isIPv4(String ipAddr){ boolean isIP=ipAddr.matches("([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}"); return isIP; } /** * 判断IPv4掩码是否合法 * @param mask * @return boolean */ public static boolean isMask(String mask){ String isMask=maskToBit(mask); if ("-1".equals(isMask)){ return false; } return true; } }
有用请点推荐!