本文介绍了Java中主机名的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主机文件(C:\ WINDOWS \ system32 \ drivers \ etcc \ hosts)有一堆主机名映射的IP地址:
My hosts file (C:\WINDOWS\system32\drivers\etc\hosts) has a bunch of IP Address to host name mappings:
# 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
I我试图找到一种通过Java API以编程方式从IPAddress转换为HostName的方法。
I am trying to find a way to convert from an IPAddress to the HostName programmatically through Java APIs.
Pseudocode:
Pseudocode:
IPAddress ip = new IPAddress("192.168.190.62");
String host = ip.getHost();
System.out.println(host); //prints "access-console"
推荐答案
我试过了来自的代码可以使用。即:
I tried the code from here and it works. Namely:
InetAddress addr = InetAddress.getByName("192.168.190.62");
String host = addr.getHostName();
System.out.println(host);
这篇关于Java中主机名的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!