java获取当前的ip地址_使用Java获取当前计算机的IP地址

在这里发布测试过的IP模糊解决方案代码https://issues.apache.org/jira/browse/JCS-40(Linux系统中的InetAddress.getLocalhost()模棱两可):/**

* Returns an InetAddress object encapsulating what is most likely the machine's LAN IP address.

* This method is intended for use as a replacement of JDK method InetAddress.getLocalHost, because

* that method is ambiguous on Linux systems. Linux systems enumerate the loopback network interface the same

* way as regular LAN network interfaces, but the JDK InetAddress.getLocalHost method does not

* specify the algorithm used to select the address returned under such circumstances, and will often return the

* loopback address, which is not valid for network communication. Details

* here.

* This method will scan all IP addresses on all network interfaces on the host machine to determine the IP address

* most likely to be the machine's LAN address. If the machine has multiple IP addresses, this method will prefer

* a site-local IP address (e.g. 192.168.x.x or 10.10.x.x, usually IPv4) if the machine has one (and will return the

* first site-local address if the machine has more than one), but if the machine does not hold a site-local

* address, this method will return simply the first non-loopback address found (IPv4 or IPv6).

* If this method cannot find a non-loopback address using this selection algorithm, it will fall back to

* calling and returning the result of JDK method InetAddress.getLocalHost.

*

* @throws UnknownHostException If the LAN address of the machine cannot be found.

*/private static InetAddress getLocalHostLANAddress() throws UnknownHostException {

try {

InetAddress candidateAddress = null;

// Iterate all NICs (network interface cards)...

for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {

NetworkInterface iface = (NetworkInterface) ifaces.nextElement();

// Iterate all IP addresses assigned to each card...

for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {

InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();

if (!inetAddr.isLoopbackAddress()) {

if (inetAddr.isSiteLocalAddress()) {

// Found non-loopback site-local address. Return it immediately...

return inetAddr;

}

else if (candidateAddress == null) {

// Found non-loopback address, but not necessarily site-local.

// Store it as a candidate to be returned if site-local address is not subsequently found...

candidateAddress = inetAddr;

// Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,

// only the first. For subsequent iterations, candidate will be non-null.

}

}

}

}

if (candidateAddress != null) {

// We did not find a site-local address, but we found some other non-loopback address.

// Server might have a non-site-local address assigned to its NIC (or it might be running

// IPv6 which deprecates the "site-local" concept).

// Return this non-loopback candidate address...

return candidateAddress;

}

// At this point, we did not find a non-loopback address.

// Fall back to returning whatever InetAddress.getLocalHost() returns...

InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();

if (jdkSuppliedAddress == null) {

throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");

}

return jdkSuppliedAddress;

}

catch (Exception e) {

UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e);

unknownHostException.initCause(e);

throw unknownHostException;

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值