我想在java中查找客户端计算机名称。我的应用程序在Intranet中运行。所以我使用下面的代码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();有一段时间没有提供主机名?你的帮助是非常appriciated。


vishnu