java中的客户端计算机名称
我想在java中找到客户端计算机名称。我的应用程序在内联网中运行。所以我使用下面的代码
public String findClientComputerName(HttpServletRequest request) {
String computerName = null;
String remoteAddress = request.getRemoteAddr();
System.out.println("remoteAddress: " + remoteAddress);
try {
InetAddress inetAddress = InetAddress.getByName(remoteAddress);
System.out.println("inetAddress: " + inetAddress);
computerName = inetAddress.getHostName();
System.out.println("computerName: " + computerName);
if (computerName.equalsIgnoreCase("localhost")) {
computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
}
} catch (UnknownHostException e) {
log.error("UnknownHostException detected in StartAction. ", e);
}
if(StringUtils.trim(computerName).length()>0) computerName = computerName.toUpperCase();
System.out.println("computerName: " + computerName);
return computerName;
}
,但有时我可以正确获取主机名,但有时却不能。我得到了正确的IP。这可能是什么原因?为什么 inetAddress.getHostName();
无法提供主机名?非常感谢您的帮助。
I want to find client computer name in java. My applciation runs in intranet. so i am using below code
public String findClientComputerName(HttpServletRequest request) {
String computerName = null;
String remoteAddress = request.getRemoteAddr();
System.out.println("remoteAddress: " + remoteAddress);
try {
InetAddress inetAddress = InetAddress.getByName(remoteAddress);
System.out.println("inetAddress: " + inetAddress);
computerName = inetAddress.getHostName();
System.out.println("computerName: " + computerName);
if (computerName.equalsIgnoreCase("localhost")) {
computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
}
} catch (UnknownHostException e) {
log.error("UnknownHostException detected in StartAction. ", e);
}
if(StringUtils.trim(computerName).length()>0) computerName = computerName.toUpperCase();
System.out.println("computerName: " + computerName);
return computerName;
}
but sometimes i am getting host name properly but some time not. I am getting Correct IP. What may be the reason for this? Why inetAddress.getHostName();
is failing to give host name some time? Your help is very appriciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
上面的函数在内网中正确返回主机名。对于本地,它将返回 localhost。
要获取本地主机的名称,我们使用computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
above function returning host name correctly in Intranet. for local it will return localhost.
To get the name for local host we use
computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
HttpServletRequest 将返回访问您的 servlet 的人的 IP 地址(v4 或 v6)。该地址可能会也可能不会解析为有效的主机名。 InetAddress.getHostName() 对 IP 地址进行反向 DNS 解析。不需要每个分配的 IP 地址都映射回有效的 DNS 条目。事实上,世界上有很大一部分 IP 地址无法解析为主机名。
您可以在 Linux 机器上使用“host”命令来查找给定 IP 地址的反向 DNS 条目(如果有),从而看到同样的结果。
HttpServletRequest will return the IP address (either v4 or v6) of whoever is hitting your servlet. That address may or may not resolve to a valid hostname. InetAddress.getHostName() does a reverse DNS resolution of the IP address. It is not required that each IP address allocated maps back to a valid DNS entry. There are, in fact, a large percent of IP addresses in the world that will not resolve to a hostname.
You can see the same thing using the 'host' command on a linux box to look up the reverse DNS entry (if any) for a given IP address.
如果 InetAddress 对象是使用主机名初始化的,则 InetAddress.getHostName() 函数将返回主机名。否则,它将执行反向 DNS 查找来获取主机名。
要使反向 DNS 查找起作用,您需要确保 Intranet 上的所有客户端都配置了主机名,并且您的 DNS 提供商(例如路由器)将主机名与其记录正确匹配。 (有些路由器可以自动执行此操作。)
The InetAddress.getHostName() function will return the host name if the InetAddress object was initialized with a host name. Otherwise, it'll do a reverse DNS lookup to get the host name.
To get this reverse DNS lookup to work, you'll need to make sure all of the clients on your intranet are configured with host names and that your DNS provider (e.g. your router) properly matches up the host names with its records. (Some routers can do this automatically.)
为了获取 Windows 计算机的主机名,您需要对 IP 地址执行反向 NetBIOS 查找。 Windows 使用名为 WINS 的系统为其计算机提供主机名。该系统基于NetBIOS。
如果您不想尝试查找协议规范并自己实现它,那么您将需要执行命令
nbtstat -A [ip地址]
(如果您使用的是Windows),或者< code>nmblookup -A [ip 地址] 如果您使用的是 Linux 计算机。如果您使用的是 Linux 计算机,则必须安装 Samba 软件包,因为所有 Linux 计算机上都安装了nmblookup
可执行文件。然后,您必须解析该命令的输出才能获取主机名。另一种方法是,如前所述,尝试找到协议规范,并实现您需要实现的部分。
In order to get the hostname of windows machines, you will need to perform a reverse NetBIOS lookup on the IP address. Windows uses a system called WINS to provide hostnames to its computers. This system is based off NetBIOS.
If you don't want to try to find a specification of the protocol and implement it yourself, then you will want to execute the command
nbtstat -A [ip address]
if you are on Windows, ornmblookup -A [ip address]
if you are on a Linux machine. If you are on a Linux machine, the Samba package will have to be installed as thenmblookup
executable is installed on all Linux machines. You will then have to parse the output of that command to get the host name.The alternative is, as stated before, try to find a specification of the protocol, and implement the part that you need to implement.