zoukankan      html  css  js  c++  java
  • java取得网卡地址

    String macStr = "";//MAC网卡地址
    try {
        InetAddress address = InetAddress.getLocalHost();//取得本地Ip地址
        System.out.println("getLocalHost:" + address.toString());
        //InetAddress address = InetAddress.getByName("192.168.46.53");
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        if (ni != null) {
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                for (int i = 0; i < mac.length; i++) {
                    System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                    macStr = macStr + String.format("%02X%s",mac[i],(i < mac.length - 1) ? "-" : "");//格式化输出
                }
            } else {
                System.out.println("Address doesn't exist or is not accessible.");
            }
        } else {
            System.out.println("Network Interface for the specified address is not found.");
    }
    } catch (UnknownHostException ex) {
        ex.printStackTrace();
    } catch (SocketException ex1) {
        ex1.printStackTrace();
    }
    System.out.println("macStr:" + macStr);
  • 相关阅读:
    linux系统安装CRT上传插件
    一些常用的vim编辑器快捷键:
    IDS 源镜像端口添加
    查看windows、linux的SN
    Tomcat安装
    puppet配置问题统计
    使用puppet
    安装puppet
    XML 生成一个XML文件
    winform 重新启动程序(重新登录)
  • 原文地址:https://www.cnblogs.com/liuzhengdao/p/1918696.html
Copyright © 2011-2022 走看看