集册 Java实例教程 获取Mac地址

获取Mac地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

525
获取Mac地址
/** 来 自 时 代      J a v a   公   众 号 - nowjava.com**/


import org.apache.log4j.Logger;

import java.net.NetworkInterface;

import java.net.SocketException;


public class Main{

    private static final Logger logger = Logger.getLogger(MacUtil.class);

    public static String getMacAddress() {

        String MacAddr = "";

        String str = "";

        try {

            NetworkInterface NIC = NetworkInterface.getByName("eth0");

            byte[] buf = NIC.getHardwareAddress();

            if (buf != null) {

                for (int i = 0; i < buf.length; i++) {

                    str = str + byteHEX(buf[i]);// 来自 N o w J a v a . c o m - 时代Java

                }

                MacAddr = str.toUpperCase();

            }

        } catch (SocketException e) {

            logger.error("MacUtil -> getMacAddress error !"

                    + e.getMessage());

        }

        return MacAddr;

    }

    public static String byteHEX(byte ib) {

        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

                'a', 'b', 'c', 'd', 
展开阅读全文