java怎么判断某个域名是否能够被成功解析成IP地址
时间: 2023-10-09 18:07:04 浏览: 82
Java可以使用InetAddress类来判断某个域名是否能够被成功解析成IP地址。使用InetAddress类的getByName()方法,将域名作为参数传入,如果返回的InetAddress对象不为null,则说明该域名能够被成功解析成IP地址。示例代码如下:
```java
import java.net.InetAddress;
public class CheckDomain {
public static void main(String[] args) {
String domain = "www.baidu.com";
try {
InetAddress address = InetAddress.getByName(domain);
if (address != null) {
System.out.println(domain + " can be resolved to IP address: " + address.getHostAddress());
} else {
System.out.println(domain + " cannot be resolved to IP address.");
}
} catch (Exception e) {
System.out.println(domain + " cannot be resolved to IP address.");
}
}
}
```
阅读全文