目录

记Java如何获取MAC地址

记Java如何获取MAC地址

MAC地址简单认识

MAC地址也叫物理地址、硬件地址,由网络设备制造商生产时烧录在网卡(Network lnterface Card)的EPROM(一种闪存芯片,通常可以通过程序擦写)。

IP地址与MAC地址在计算机里都是以二进制表示的,IP地址是32位的,而MAC地址则是48位的 ,通常表示为12个16进制数,如:00-16-EA-AE-3C-40就是一个MAC地址,其中前3个字节,16进制数00-16-EA代表网络硬件制造商的编号,它由IEEE(电气与电子工程师协会)分配,而后3个字节,16进制数AE-3C-40代表该制造商所制造的某个网络产品(如网卡)的系列号。只要不更改自己的MAC地址,MAC地址在世界是唯一的。形象地说,MAC地址就如同身份证上的身份证号码,具有唯一性 。

MAC地址用于在网络中唯一标示一个网卡,一台设备若有一或多个网卡,则每个网卡都需要并会有一个唯一的MAC地址。

查看本机MAC地址

打开命令窗口:win + R, 然后输入cmd,回车

https://i-blog.csdnimg.cn/blog_migrate/fd89ff8ecc50557f785f0634bbb3b87a.png

输入 ipconfig /all 查看MAC地址

https://i-blog.csdnimg.cn/blog_migrate/487deb01b8036c24b1934e7b3e40cb65.png

Java获取mac地址的两种方式

1.通过InetAddress对象获取

package com.chunni.mac;

import org.junit.jupiter.api.Test;

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


public class MACTest {

    @Test
    public void test01 () throws UnknownHostException, SocketException {
        // 获取特定的mac地址
        InetAddress address = InetAddress.getByName("xxx");
        byte[] mac = NetworkInterface.getByInetAddress(address).getHardwareAddress();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            // 转为16进制
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        System.out.println(sb.toString());
    }
    
    @Test
    public void test02 () throws UnknownHostException, SocketException {
        InetAddress[] addresses = InetAddress.getAllByName("xxx");
        StringBuilder sb = new StringBuilder();
        for (InetAddress address : addresses) {
            byte[] mac = NetworkInterface.getByInetAddress(address).getHardwareAddress();
            for (int i = 0; i < mac.length; i++) {
                // 转为16进制
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "\n"));
            }
        }
        System.out.println(sb.toString());

    }
}

2.通过NetworkInterface对象获取

package com.chunni.mac;

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

public class MACTest01 {

    public static void main(String[] args) throws SocketException {
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        byte[] mac = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = allNetInterfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                continue;
            } else {
                mac = netInterface.getHardwareAddress();
                if (mac != null) {
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "\n"));
                    }
                }
            }
        }
        System.out.println(sb.toString());
    }
}

注意:第一种方式查询所有mac地址时会有重复的,因为ipv4和ipv6都会查询一份mac地址