Java获取IP地址和MAC地址方法详解(附演示示例)

目录

  • 前言
  • 1. IP及MAC
  • 2. 特定适配器
  • 前言

    需要获取客户端的IP地址以及MAC地址

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class test {
    
        public static void main(String[] args) {
            try {
                // 执行命令
                Process process = Runtime.getRuntime().exec("ipconfig /all");
                // 读取命令输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    // 输出命令结果
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    纯用CMD可能权限不够,可能格式可能乱码等问题

    后续转为网络编程API接口

    网络适配器的 IPv4 和 MAC 地址,最好直接使用 Java 的网络编程 API,而不是通过执行系统命令来获取,可以使用 java.net.NetworkInterface 类来获取网络接口的信息,然后进一步筛选出所需的适配器信息

  • 在获取本地主机信息时,要考虑多网卡的情况,确保准确获取所需的网络适配器信息
  • 对于操作系统信息的获取,可以考虑使用更可靠的方式,如 System.getProperty() 方法
  • 1. IP及MAC

    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.Enumeration;
    
    public class test {
    
        public static void main(String[] args) {
            try {
                // 获取所有网络接口
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                while (interfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = interfaces.nextElement();
                    // 输出网络接口名称
                    System.out.println("Network Interface: " + networkInterface.getDisplayName());
                    // 获取该网络接口的所有地址
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        // 输出地址信息
                        System.out.println("  Address: " + address.getHostAddress());
                    }
                    // 获取 MAC 地址
                    byte[] mac = networkInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                        }
                        // 输出 MAC 地址
                        System.out.println("  MAC Address: " + sb.toString());
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }
    

    截图如下:

    2. 特定适配器

    不同电脑可能特定的适配器不大一样,具体Demo看自身

    package com.example.test;
    
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.Enumeration;
    
    public class test {
    
        public static void main(String[] args) {
            try {
                // 获取所有网络接口
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                while (interfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = interfaces.nextElement();
                    // 检查是否是你想要的网络适配器,这里假设名字为 "VMware Network Adapter VMnet8"
                    if (networkInterface.getDisplayName().equals("VMware Virtual Ethernet Adapter for VMnet8")) {
                        // 获取该网络接口的所有地址
                        Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                        while (addresses.hasMoreElements()) {
                            InetAddress address = addresses.nextElement();
                            // 过滤 IPv4 地址
                            if (address instanceof java.net.Inet4Address) {
                                // 输出 IPv4 地址
                                System.out.println("IPv4 Address: " + address.getHostAddress());
                            }
                        }
                        // 获取 MAC 地址
                        byte[] mac = networkInterface.getHardwareAddress();
                        if (mac != null) {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < mac.length; i++) {
                                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                            }
                            // 输出 MAC 地址
                            System.out.println("MAC Address: " + sb.toString());
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }
    

    截图如下:

    作者:码农研究僧

    物联沃分享整理
    物联沃-IOTWORD物联网 » Java获取IP地址和MAC地址方法详解(附演示示例)

    发表回复