如何从Java中的域名获取IP地址?

我正在编写一个需要IP地址的应用程序。 我有一个域名,我想知道如何从中获取IP地址。 例如,“www.girionjava.com”。 如何通过Java编程获取本网站的IP地址? 谢谢。

InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com"); 

然后,如果您想将IP作为字符串

 String address = giriAddress.getHostAddress(); 
 InetAddress.getByName("www.girionjava.com") 

这应该很简单。

 InetAddress[] machines = InetAddress.getAllByName("yahoo.com"); for(InetAddress address : machines){ System.out.println(address.getHostAddress()); } 

(打印正弦java中的额外掩码认为所有整数都要签名,但IP地址是无符号的)

 InetAddress[] machines = InetAddress.getAllByName("yahoo.com"); for(InetAddress address : machines){ byte[] ip = address.getAddress(); for(byte b : ip){ System.out.print(Integer.toString(((int)b)&0xFF)+"."); } System.out.println(); }