杰瑞科技汇

Java如何获取Linux本机IP?

在Java中获取Linux本机IP地址

在Java中获取Linux本机IP地址有几种方法,以下是几种常见的实现方式:

Java如何获取Linux本机IP?-图1
(图片来源网络,侵删)

方法1:使用NetworkInterface类(推荐)

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class GetLocalIP {
    public static void main(String[] args) throws Exception {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (networkInterface.isUp() && !networkInterface.isLoopback()) {
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        System.out.println("本机IP: " + inetAddress.getHostAddress());
                    }
                }
            }
        }
    }
}

方法2:使用InetAddress.getLocalHost()

import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetLocalIP {
    public static void main(String[] args) {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println("本机IP: " + localHost.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

方法3:使用命令行执行ifconfig

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class GetLocalIP {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("ifconfig");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("inet ") && !line.contains("127.0.0.1")) {
                    String ip = line.trim().split("\\s+")[1];
                    System.out.println("本机IP: " + ip);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

注意事项

  1. 方法1是最推荐的方式,因为它不依赖于系统命令,跨平台性好
  2. 方法2简单但可能返回127.0.0.1(回环地址)
  3. 方法3依赖于Linux的ifconfig命令,在其他系统上可能不适用
  4. 如果有多个网络接口(如eth0, wlan0等),上述方法可能返回多个IP地址
  5. 对于IPv6地址,需要额外处理

获取特定网络接口的IP

如果需要获取特定网络接口(如eth0)的IP:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class GetSpecificInterfaceIP {
    public static void main(String[] args) throws Exception {
        String interfaceName = "eth0"; // 指定网络接口名称
        NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
        if (networkInterface != null) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                    System.out.println(interfaceName + "的IP: " + inetAddress.getHostAddress());
                }
            }
        } else {
            System.out.println("未找到网络接口: " + interfaceName);
        }
    }
}

方法都可以在Linux环境下获取本机IP地址,选择哪种方法取决于你的具体需求和环境。

Java如何获取Linux本机IP?-图2
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇