mport java.net.InetAddress;
import java.net.UnknownHostException;
public class GetIPAddressFromHostname {
public static void main(String[] args) {
try {
InetAddress inetAddr = InetAddress.getByName("open-open.com");
byte[] addr = inetAddr.getAddress();
// Convert to dot representation
String ipAddr = "";
for (int i = 0; i < addr.length; i++) {
if (i > 0) {
ipAddr += ".";
}
ipAddr += addr[i] & 0xFF;
}
System.out.println("IP Address: " + ipAddr);
}
catch (UnknownHostException e) {
System.out.println("Host not found: " + e.getMessage());
}
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10994.html