Java获取当前IP地址的多种方式

在Java中,获取当前IP地址有多种方式。本文将介绍几种常用的方法,并提供相应的代码示例。

1. 使用InetAddress类获取当前IP地址

Java提供了InetAddress类来获取IP地址。InetAddress类是一个与IP地址相关的类,可以通过该类获取本地主机的IP地址。

import java.net.InetAddress;

public class GetIPAddress {
    public static void main(String[] args) {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            String ipAddress = localHost.getHostAddress();
            System.out.println("当前IP地址:" + ipAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上述代码中,使用getLocalHost()方法获取本地主机的InetAddress对象,然后调用getHostAddress()方法获取IP地址。

2. 使用NetworkInterface类获取当前IP地址

除了InetAddress类,还可以使用NetworkInterface类来获取当前IP地址。NetworkInterface类提供了网络接口的信息,包括IP地址、子网掩码、MAC地址等。

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class GetIPAddress {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        String ipAddress = inetAddress.getHostAddress();
                        System.out.println("当前IP地址:" + ipAddress);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上述代码中,使用getNetworkInterfaces()方法获取所有网络接口的枚举,然后遍历每个网络接口,再遍历每个网络接口的IP地址。

3. 使用第三方库获取当前IP地址

除了使用Java自带的类库,还可以使用第三方库简化获取IP地址的操作。例如,可以使用Apache Commons Net库中的InetAddressUtils类来获取IP地址。

import org.apache.commons.net.util.InetAddressUtils;

public class GetIPAddress {
    public static void main(String[] args) {
        try {
            String ipAddress = InetAddressUtils.getLocalHostLANAddress().getHostAddress();
            System.out.println("当前IP地址:" + ipAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上述代码中,使用getLocalHostLANAddress()方法获取本地主机的IP地址。

总结

本文介绍了三种常用的方式来获取Java中的当前IP地址。通过InetAddress类、NetworkInterface类或第三方库,我们可以轻松获取IP地址并进行相应的处理。

流程图

下面是获取当前IP地址的流程图:

flowchart TD
    start[开始]
    input[使用InetAddress类获取IP地址]
    output1[输出IP地址]
    input-->output1
    input2[使用NetworkInterface类获取IP地址]
    output2[输出IP地址]
    input2-->output2
    input3[使用第三方库获取IP地址]
    output3[输出IP地址]
    input3-->output3
    end[结束]
    output1-->end
    output2-->end
    output3-->end

甘特图

下面是获取当前IP地址的甘特图:

gantt
    dateFormat  YYYY-MM-DD
    title 获取当前IP地址
    section 获取IP地址
    使用InetAddress类获取IP地址   :done, 2022-12-31, 1d
    使用NetworkInterface类获取IP地址  :done, 2022-12-31, 1d
    使用第三方库获取IP地址   :done, 2022-12-31, 1d