public class InetAddresTest {
public static void main(String ... agrs) {
try {
InetAddress inet = InetAddress.getByName("1.2");
System.out.println("Good ip address");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BTW các InetAddress sản xuất địa chỉ ip trở lại là "1.0.0.2". Tôi không thể tìm thấy một câu trả lời hợp lý từ javadoc của InetAddress. Bạn có thể giải thích về hành vi này không?Làm thế nào đến InetAddress.getByName ("1.2") là địa chỉ IP hợp lệ?
Brian là tại chỗ trên với câu trả lời của mình. Nếu mục tiêu của bạn là xác thực địa chỉ IP thì tôi khuyên bạn nên sử dụng Apache-Commons [InetAddressValidator] (http://commons.apache.org/validator/apidocs/org/apache/commons/validator/routines/InetAddressValidator.html) – CoolBeans
@ CoolBeans - thêm rằng để trả lời, với một thay thế cũng - cảm ơn. –
Apache Commons InetAddressValidator chỉ có thể trợ giúp nếu bạn đang xác thực IPv4, vì bản phát hành hiện tại (1.4) dường như không hỗ trợ IPv6. – Douglas