import org.apache.commons.validator.routines.InetAddressValidator; public class Main { public static void main(String[] args) { String ipAddress = "192.168.1.1"; boolean isValidIPv4 = isValidIPv4(ipAddress); if (isValidIPv4) { System.out.println(ipAddress + " 是一个有效的IPv4地址。"); } else { System.out.println(ipAddress + " 不是一个有效的IPv4地址。"); } } public static boolean isValidIPv4(String ipAddress) { InetAddressValidator validator = InetAddressValidator.getInstance(); return validator.isValidInet4Address(ipAddress); } }