Java特定於主機的IP地址
Java如何將主機名改為其特定的IP地址?
解決方法
下麵的示例演示如何net.InetAddress類的InetAddress.getByName()方法將主機名變更其特定的IP地址。
import java.net.InetAddress; import java.net.UnknownHostException; public class GetIP { public static void main(String[] args) { InetAddress address = null; try { address = InetAddress.getByName ("www.gitbook.net"); } catch (UnknownHostException e) { System.exit(2); } System.out.println(address.getHostName() + "=" + address.getHostAddress()); System.exit(0); } }
結果
上麵的代碼示例將產生以下結果。
http://www.gitbook.net = 112.124.103.85