inetaddress.getlocalhost()方法用于获取本地ip地址,但其可靠性存在问题。
代码示例:
public static void main(String[] args) throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: " + addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: " + hostname);
}在Mac上运行上述代码的输出:

在Windows环境下,虽然InetAddress.getLocalHost()方法看起来能够正常获取本地IP,但实际上在多网卡协同工作的环境下是不准确的。默认情况下,本机名是localhost,在hosts文件中对应的IP是127.0.0.1,因此通过该函数获取的IP通常是127.0.0.1。这是因为该函数简单地读取/etc/hosts的内容,所以默认返回127.0.0.1,这非常不可靠。因此,不建议在生产环境中使用这种方法。
立即学习“Java免费学习笔记(深入)”;
让我们来看一下/etc/hosts文件的内容:

获取本地IP的更可靠方法是使用NetworkInterface类。该类提供了三个方法:getName()用于获取网络接口名,getDisplayName()用于获取网络接口别名,以及getInetAddresses()用于获取与网络接口绑定的所有IP地址。
以下是适用于Windows和Linux的通用代码,用于获取本机IP:
package test;
<p>import java.io.IOException;
import java.net.*;
import java.util.Enumeration;</p><p>/**</p><ul><li><p>@author yanZhiHang</p></li><li><p>@date 2023/2/2 11:59
*/
public class GetLocalHost {
public static void main(String[] args) throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: " + addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: " + hostname);
System.out.println("本机ip:" + getIpAddress());
}</p><p>public static String getIpAddress() {
try {
// 从网卡中获取IP
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
// 用于排除回送接口,非虚拟网卡,未在使用中的网络接口
if (!netInterface.isLoopback() && !netInterface.isVirtual() && netInterface.isUp()) {
// 返回和网络接口绑定的所有IP地址
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = addresses.nextElement();
if (ip instanceof Inet4Address) {
return ip.getHostAddress();
}
}
}
}
} catch (Exception e) {
System.err.println("IP地址获取失败" + e.toString());
}
return "";
}
}注意事项:代码中返回的是与网络接口绑定的所有IP地址。在使用Docker容器的服务器上,根据上述代码获取的可能是Docker对外的网卡IP,这可能会导致获取到错误的IP。此外,在有多个网卡的情况下,也可能影响获取真正的IP。
为了解决这个问题,因为我的真实目的是验证输入的IP是否为本机IP,所以只要证明网络接口中的所有IP包含输入的IP即可。以下是改造后的代码:
public static boolean isLocalHost(String localHost) throws Exception {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
if (!netInterface.isLoopback() && !netInterface.isVirtual() && netInterface.isUp()) {
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (null != ip && ip.getHostAddress().contains(localHost)) {
return true;
}
}
}
}
} catch (Exception e) {
log.error("校验IP地址失败:", e.getCause());
e.printStackTrace();
throw new Exception(e);
}
return false;
}以上就是java:获取本机IP,Linux环境下使用InetAddress.getLocalHost()方法获得127.0.0.1的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号