Java中主机名的IP地址?

mai*_*rgs 22 java hosts ip-address hostname

我的主机文件(C:\ WINDOWS\system32\drivers\etc\hosts)有一堆主机名映射的IP地址:

# Switches
192.168.200.254       sw-con-ctrl
192.168.201.253    sw-con-ctrl-2
192.168.201.254       sw-con-ctrl-1
# 192.168.188.1       sw-con-ctrl-blk-1
# 192.168.189.1       sw-con-ctrl-red
192.168.190.62        access-console

# Routers
192.168.21.1          rtr1
192.168.22.1          rtr2
Run Code Online (Sandbox Code Playgroud)

我试图找到一种通过Java API以编程方式从IPAddress转换为HostName的方法.

伪代码:

IPAddress ip = new IPAddress("192.168.190.62");
String host = ip.getHost();
System.out.println(host);  //prints "access-console"
Run Code Online (Sandbox Code Playgroud)

use*_*509 50

我从这里尝试了代码,它的工作原理.即:

  InetAddress addr = InetAddress.getByName("192.168.190.62");
  String host = addr.getHostName();
  System.out.println(host);
Run Code Online (Sandbox Code Playgroud)