Java根据IP地址/掩码位(CIDR)和起始IP-终止IP计算所有IP地址(IPV4和IPV6)
计算IP列表
IPV4
1、需要的jar包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.7</version>
</dependency>
2、代码示例
private static R getIpsV4ByCidr(String cidr) {
String ip = cidr.split("/")[0];
int mask = Integer.parseInt(cidr.split("/")[1]);
String ipFrom = Ipv4Util.getBeginIpStr(ip, mask);
String ipTo = Ipv4Util.getEndIpStr(ip, mask);
if (Ipv4Util.countByIpRange(ipFrom, ipTo) > 1024) {
return R.warn("最多可以支持1024个IP");
}
return R.ok().addData(Ipv4Util.list(ip, mask, true));
}
private static R getIpsV4ByRange(String ipFrom, String ipTo) {
if (Ipv4Util.countByIpRange(ipFrom, ipTo) > 1024) {
return R.warn("最多可以支持1024个IP");
}
return R.ok().addData(Ipv4Util.list(ipFrom, ipTo));
}
IPV6
1、需要的jar包
<dependency>
<groupId>com.googlecode.java-ipv6</groupId>
<artifactId>java-ipv6</artifactId>
<version>0.17</version>
</dependency>
2、代码示例
private static R getIpsV6ByCidr(String cidr) {
List<String> ipv6List = Lists.newArrayList();
IPv6Network network = IPv6Network.fromString(cidr);
if (network.size().compareTo(new BigInteger(String.valueOf(1024))) > 0) {
return R.warn("最多可以支持1024个IP");
}
network.forEach(e -> ipv6List.add(e.toString()));
return R.ok().addData(ipv6List);
}
private static R getIpsV6ByRange(String ipFrom, String ipTo) {
List<String> ipv6List = Lists.newArrayList();
IPv6AddressRange ipv6Range = IPv6AddressRange.fromFirstAndLast(IPv6Address.fromString(ipFrom), IPv6Address.fromString(ipTo));
if (ipv6Range.size().compareTo(new BigInteger(String.valueOf(1024))) > 0) {
return R.warn("最多可以支持1024个IP");
}
ipv6Range.forEach(e -> ipv6List.add(e.toString()));
return R.ok().addData(ipv6List);
}
IP校验
IPV4
public static boolean fieldIPv4Valid(String matchParams) {
String ipv4Format = "^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$";
boolean flag = matchParams.matches(ipv4Format);
return flag;
}
IPV6
public static boolean fieldIPv6Valid(String matchParams) {
String ipv6Format = "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$";
boolean flag = matchParams.matches(ipv6Format);
return flag;
}
|