5种Java获取WiFi IP地址和MAC地址的方法及代码示例

本文介绍了5种使用Java获取WiFi IP地址和MAC地址的方法,并提供了完整的代码示例。涵盖了使用InetAddress类、Apache Commons Net库、Socket类、NetworkInterface类以及Runtime类和ipconfig命令等方法。

1. 使用Java自带的InetAddress类获取IP地址和MAC地址:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class WifiInfo {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            System.out.println('IP Address: ' + address.getHostAddress());

            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
            byte[] mac = networkInterface.getHardwareAddress();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format('%02X%s', mac[i], (i < mac.length - 1) ? '-' : ''));
            }
            System.out.println('MAC Address: ' + sb.toString());
        } catch (UnknownHostException | SocketException e) {
            e.printStackTrace();
        }
    }
}

2. 使用Apache Commons Net库中的NetworkUtils类获取IP地址和MAC地址:

下载地址:http://commons.apache.org/proper/commons-net/download_net.cgi

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

public class WifiInfo {
    public static void main(String[] args) {
        NetworkUtils networkUtils = new NetworkUtils();
        String ipAddress = networkUtils.getIPAddress();
        System.out.println('IP Address: ' + ipAddress);

        String macAddress = networkUtils.getMacAddress();
        System.out.println('MAC Address: ' + macAddress);
    }
}

3. 使用Java的Socket和InetAddress类获取IP地址和MAC地址:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.io.IOException;

public class WifiInfo {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket('google.com', 80);
            InetAddress address = socket.getLocalAddress();
            System.out.println('IP Address: ' + address.getHostAddress());

            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
            byte[] mac = networkInterface.getHardwareAddress();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format('%02X%s', mac[i], (i < mac.length - 1) ? '-' : ''));
            }
            System.out.println('MAC Address: ' + sb.toString());
        } catch (UnknownHostException | SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4. 使用Java的NetworkInterface类获取IP地址和MAC地址:

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

public class WifiInfo {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                if (networkInterface.isUp() && !networkInterface.isLoopback()) {
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (address.getHostAddress().contains('.')) {
                            System.out.println('IP Address: ' + address.getHostAddress());

                            byte[] mac = networkInterface.getHardwareAddress();
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < mac.length; i++) {
                                sb.append(String.format('%02X%s', mac[i], (i < mac.length - 1) ? '-' : ''));
                            }
                            System.out.println('MAC Address: ' + sb.toString());
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

5. 使用Java的Runtime类和ipconfig命令获取IP地址和MAC地址:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WifiInfo {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec('ipconfig /all');
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            String ipAddress = null;
            String macAddress = null;
            while ((line = input.readLine()) != null) {
                if (line.contains('IPv4 Address')) {
                    ipAddress = line.substring(line.lastIndexOf(':') + 2);
                } else if (line.contains('Physical Address')) {
                    macAddress = line.substring(line.lastIndexOf(':') + 2);
                }
            }

            System.out.println('IP Address: ' + ipAddress);
            System.out.println('MAC Address: ' + macAddress);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意:

  • 以上代码示例在Windows系统中测试通过,在其他操作系统中可能需要修改ipconfig命令。
  • 使用Runtime类执行系统命令可能存在安全风险,请谨慎使用。
  • 获取MAC地址可能会受到系统权限限制,需要根据具体情况进行调整。
5种Java获取WiFi IP地址和MAC地址的方法及代码示例

原文地址: https://www.cveoy.top/t/topic/mPk8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录