主机IP地址0:0:0:0:0:0:0:1在Servlet上的应用

3

我正在尝试使用以下方法获取用户的主机名、IP地址和端口:

request.getRemoteHost()
request.getRemoteAddr()
request.getRemotePort()

但是我得到的是:

主机名:0:0:0:0:0:0:0:1
IP地址:空地址
端口号:51111

我该如何获取正确的IP地址,注意我们的网络IP以10.100.x.x开头。

非常感谢。

1个回答

10

如果您在请求URI中使用localhost,那么IPv6地址为0:0:0:0:0:0:0:1。请不要使用localhost,而是在请求URI中使用IPv4地址以获取正确的IP。


谢谢,那就是问题所在了。我之前是从本地主机进行测试的,但是无论是request.getRemoteHost()还是request.getRemoteAddr()都返回了相同的IP地址。那我应该如何获取计算机的Netbios名称呢? - skystar7
好的,我通过以下代码实现了: InetAddress inetAddress = InetAddress.getByName(request.getRemoteHost()); String computerName = inetAddress.getHostName(); if (computerName.equalsIgnoreCase("localhost")) { computerName = InetAddress.getLocalHost().getCanonicalHostName(); } - skystar7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接