Android IPv4和IPv6格式校验
public boolean isValidIPv4(String ip) {
String[] ipParts = ip.split("\\.");
if (ipParts.length != 4) {
return false;
}
for (String s : ipParts) {
try {
int i = Integer.parseInt(s);
if ((i < 0) || (i > 255)) {
return false;
}
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
public boolean isValidIPv6(String dns) {
try {
InetAddress address = InetAddress.getByName(dns);
return address instanceof Inet6Address;
} catch (UnknownHostException e) {
return false;
}
}