栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA获取机器的MAC地址

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA获取机器的MAC地址

JAVA获取机器的MAC地址

需求:获取机器的mac地址

问题:一些国产操作系统获取不到MAC地址,以下搜集了一些获取mac地址的方法

1.获取MAC地址

ps:该方法在银河麒麟中可用

public static String getMAC() throws SocketException {
        Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();

        while(networkInterfaces.hasMoreElements()) {
            NetworkInterface network = networkInterfaces.nextElement();
            log.info("network:{}", network);
            byte[] mac = network.getHardwareAddress();
            if(mac == null) {
                log.warn("mac is null");
            } else {

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                }
                log.info("MAC address : {} ", sb);

                if (!"".equals(sb.toString())) {
                    return sb.toString();
                }
                break;
            }
        }
        return "";
    }
2.获取MAC地址

在UOS系统中可用

public static List getMacAddress() {

        ArrayList macs = new ArrayList<>();
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            byte[] mac = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                    continue;
                } else {
                    mac = netInterface.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) ? ":" : ""));
                        }
                        if (sb.length() > 0) {
                            macs.add(sb.toString().toLowerCase());
                        }
                    }
                }
            }
            log.info("获取的mac地址为:{}", macs);
            return macs;
        } catch (Exception e) {
            log.error("MAC地址获取失败", e);
        }
        return Collections.emptyList();
    }
3.获取Linux的MAC地址
public static List getLinuxMACAddress() {
        List macs = new ArrayList<>();
        BufferedReader bufferedReader = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("ifconfig -a");
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line = null;
            int index = -1;
            while ((line = bufferedReader.readLine()) != null) {
                index = line.toLowerCase().indexOf("硬件地址");
                String mac = "";
                if (index != -1) {
                    mac = line.substring(index + 4).trim();
                    macs.add(mac.toLowerCase());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            bufferedReader = null;
            process = null;
        }
        log.info("MAC address :{}", macs);
        return macs;
    }
4.获取windows的mac地址
public static List getMACAddressByWindows() throws Exception {
        ArrayList rs = new ArrayList<>();
        String result = "";
        Process process = Runtime.getRuntime().exec("ipconfig /all");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        int index = -1;
        String line;
        while ((line = br.readLine()) != null) {
            index = line.indexOf("物理地址");
            if (index >= 0) {
                index = line.indexOf(":");
                if (index >= 0) {
                    result = line.substring(index + 1).trim();
                }
                rs.add(result.toUpperCase());
            }
        }
        br.close();
        log.info("Mac address : {}",rs);
        return rs;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/944218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号