java java中的客户端计算机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3993021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Client computer name in java
提问by vishnu
I want to find client computer name in java. My applciation runs in intranet. so i am using below code
我想在java中找到客户端计算机名称。我的应用程序在 Intranet 中运行。所以我使用下面的代码
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.
但有时我会正确获取主机名,但有时不会。我得到了正确的 IP。这可能是什么原因?为什么 inetAddress.getHostName();
有时无法提供主机名?你的帮助是非常appriciated。
回答by vishnu
private String getHostName (InetAddress inaHost) throws UnknownHostException
{
try
{
Class clazz = Class.forName("java.net.InetAddress");
Constructor[] constructors = clazz.getDeclaredConstructors();
constructors[0].setAccessible(true);
InetAddress ina = (InetAddress) constructors[0].newInstance();
Field[] fields = ina.getClass().getDeclaredFields();
for (Field field: fields)
{
if (field.getName().equals("nameService"))
{
field.setAccessible(true);
Method[] methods = field.get(null).getClass().getDeclaredMethods();
for (Method method: methods)
{
if (method.getName().equals("getHostByAddr"))
{
method.setAccessible(true);
return (String) method.invoke(field.get (null), inaHost.getAddress());
}
}
}
}
} catch (ClassNotFoundException cnfe) {
} catch (IllegalAccessException iae) {
} catch (InstantiationException ie) {
} catch (InvocationTargetException ite) {
throw (UnknownHostException) ite.getCause();
}
return null;
}
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();
上述函数在 Intranet 中正确返回主机名。对于本地,它将返回 localhost。要获取我们使用的本地主机的名称computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
回答by kkress
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.
HttpServletRequest 将返回访问您的 servlet 的任何人的 IP 地址(v4 或 v6)。该地址可能会也可能不会解析为有效的主机名。InetAddress.getHostName() 对 IP 地址进行反向 DNS 解析。不需要将分配的每个 IP 地址映射回有效的 DNS 条目。事实上,世界上有很大一部分 IP 地址无法解析为主机名。
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.
您可以在 Linux 机器上使用“host”命令查找给定 IP 地址的反向 DNS 条目(如果有),也可以看到相同的内容。
回答by Tom
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.
如果 InetAddress 对象是用主机名初始化的,则 InetAddress.getHostName() 函数将返回主机名。否则,它将执行反向 DNS 查找以获取主机名。
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.)
要使这种反向 DNS 查找工作,您需要确保您的 Intranet 上的所有客户端都配置有主机名,并且您的 DNS 提供商(例如您的路由器)将主机名与其记录正确匹配。(某些路由器可以自动执行此操作。)
回答by Thomas
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 WINSto provide hostnames to its computers. This system is based off NetBIOS.
为了获得 Windows 机器的主机名,您需要对 IP 地址执行反向 NetBIOS 查找。Windows 使用称为WINS的系统为其计算机提供主机名。该系统基于 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, or nmblookup -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 the nmblookup
executable is installed on all Linux machines. You will then have to parse the output of that command to get the host name.
如果您不想尝试找到协议的规范并自己实现它,那么nbtstat -A [ip address]
如果您在 Windows 上,或者nmblookup -A [ip address]
如果您在 Linux 机器上,您将需要执行该命令。如果您使用的是 Linux 机器,则必须安装 Samba 包,因为nmblookup
所有 Linux 机器上都安装了可执行文件。然后,您必须解析该命令的输出以获取主机名。
The alternative is, as stated before, try to find a specification of the protocol, and implement the part that you need to implement.
另一种方法是,如前所述,尝试找到协议的规范,并实现您需要实现的部分。