获取域名的Ip地址

时间:2022-06-19 14:38:41
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * 获取域名的Ip地址
 * @author HuangLi
 *
 */
public class InetAddressTest {
    public static void main(String[] args){
    try {
InetAddress address1 = InetAddress.getByName("google.com");
getAddressType(address1);
InetAddress address2 = InetAddress.getByName("ipv6.google.com");
getAddressType(address2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
    }


private static void getAddressType(InetAddress address) {
System.out.println(address);
if(address instanceof Inet4Address){
System.out.println("这个IP地址是IPV4地址");
}else if(address instanceof Inet6Address){
System.out.println("这个IP地址是IPV6地址");
}
}

}

输出结果:

google.com/172.217.160.110
这个IP地址是IPV4地址