• java获取本机mac物理地址


    package com.simonjia.util.other;

    import java.net.InetAddress;
    import java.net.InterfaceAddress;
    import java.net.NetworkInterface;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;

    public class MacTools {
    /***因为一台机器不一定只有一个网卡呀,所以返回的是数组是很合理的***/
    public static List<String> getMacList() throws Exception {
    java.util.Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    StringBuilder sb = new StringBuilder();
    ArrayList<String> tmpMacList = new ArrayList<>();
    while (en.hasMoreElements()) {
    NetworkInterface iface = en.nextElement();
    List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
    for (InterfaceAddress addr : addrs) {
    InetAddress ip = addr.getAddress();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    if (network == null) {
    continue;
    }
    byte[] mac = network.getHardwareAddress();
    if (mac == null) {
    continue;
    }
    sb.delete(0, sb.length());
    for (int i = 0; i < mac.length; i++) {
    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }
    tmpMacList.add(sb.toString());
    }
    }
    if (tmpMacList.size() <= 0) {
    return tmpMacList;
    }
    /***去重,别忘了同一个网卡的ipv4,ipv6得到的mac都是一样的,肯定有重复,下面这段代码是。。流式处理***/
    List<String> unique = tmpMacList.stream().distinct().collect(Collectors.toList());
    return unique;
    }

    public static void main(String[] args) throws Exception {
    long a = System.currentTimeMillis();
    System.out.println("进行 multi net address 测试===》");
    List<String> macs = getMacList();
    long b = System.currentTimeMillis();
    System.out.println("本机的mac网卡的地址有:" + macs);
    System.out.println("总耗时----" + (b - a) + "-----ms");
    }
    }

     

    这个只能拿到服务本机的mac地址,对于远程请求获取请求者的mac信息并不适用……

    获取来访者ip信息----:https://www.cnblogs.com/SimonHu1993/p/11015069.html

    博主原文太长,只取精华-0-  详细可见:https://blog.csdn.net/cdnight/article/details/86741265

  • 相关阅读:
    BFGS(1)
    局部加权之线性回归(1)
    遗传算法(1)
    Python扩展(Cython混编)
    梯度下降算法(1)
    Python矩阵作图库matplotlib的初级使用
    搭建FTP服务器与客户端(1)
    maven常见小问题整理
    git常用命令问题整理
    maven常用命令整理
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/11014786.html
Copyright © 2020-2023  润新知