为了项目的安全,有时候需要得到电脑的唯一码,比如:网卡的mac地址。和大家分享一下,下面是项目中用到的工具类:/**

* 获取Mac地址

* @return

*/

public String getMAC() {

try {

InetAddress ia = InetAddress.getLocalHost();

byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

//System.out.println("mac数组长度:"+mac.length);

StringBuffer sb = new StringBuffer("");

for(int i=0; i

if(i!=0) {

sb.append("-");

}

//字节转换为整数

int temp = mac[i]&0xff;

String str = Integer.toHexString(temp);

if(str.length()==1) {

sb.append("0"+str);

}else {

sb.append(str);

}

}

String macStr = sb.toString().toUpperCase();

return macStr;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

当然除了上面的简单方法还有如下网友提供的方法可以尝试import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.net.NetworkInterface;

public class MacAddressAPI {

/**

* 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.

*/

public static String getOSName() {

return System.getProperty("os.name").toLowerCase();

}

/**

* 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取. 如果有特殊系统请继续扩充新的取mac地址方法.

* @return mac地址

*/

public static String getUnixMACAddress() {

String mac = null;

BufferedReader bufferedReader = null;

Process process = null;

try {

// linux下的命令,一般取eth0作为本地主网卡

process = Runtime.getRuntime().exec("ifconfig eth0");

// 显示信息中包含有mac地址信息

bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = null;

int index = -1;

while ((line = bufferedReader.readLine()) != null) {

// 寻找标示字符串[hwaddr]

index = line.toLowerCase().indexOf("hwaddr");

if (index >= 0) {// 找到了

// 取出mac地址并去除2边空格

mac = line.substring(index + "hwaddr".length() + 1).trim();

break;

}

}

} catch (IOException e) {

System.out.println("unix/linux方式未获取到网卡地址");

} finally {

try {

if (bufferedReader != null) {

bufferedReader.close();

}

} catch (IOException e1) {

e1.printStackTrace();

}

bufferedReader = null;

process = null;

}

return mac;

}

/**

* 获取widnows网卡的mac地址.

* @return mac地址

*/

public static String getWindowsMACAddress() {

String mac = null;

BufferedReader bufferedReader = null;

Process process = null;

try {

// windows下的命令,显示信息中包含有mac地址信息

process = Runtime.getRuntime().exec("ipconfig /all");

bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = null;

int index = -1;

while ((line = bufferedReader.readLine()) != null) {

// 寻找标示字符串[physical

index = line.toLowerCase().indexOf("physical address");

if (index >= 0) {// 找到了

index = line.indexOf(":");// 寻找":"的位置

if (index >= 0) {

// 取出mac地址并去除2边空格

mac = line.substring(index + 1).trim();

}

break;

}

}

} catch (IOException e) {

System.out.println("widnows方式未获取到网卡地址");

} finally {

try {

if (bufferedReader != null) {

bufferedReader.close();

}

} catch (IOException e1) {

e1.printStackTrace();

}

bufferedReader = null;

process = null;

}

return mac;

}

/**

* windows 7 专用 获取MAC地址

* @return

* @throws Exception

*/

public static String getWindows7MACAddress() {

StringBuffer sb = new StringBuffer();

try {

// 获取本地IP对象

InetAddress ia = InetAddress.getLocalHost();

// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

// 下面代码是把mac地址拼装成String

for (int i = 0; i

// mac[i] & 0xFF 是为了把byte转化为正整数

String s = Integer.toHexString(mac[i] & 0xFF);

sb.append(s.length() == 1 ? 0 + s : s);

}

} catch (Exception ex) {

System.out.println("windows 7方式未获取到网卡地址");

}

return sb.toString();

}

/**

* 获取MAC地址

* @param argc 运行参数.

* @throws Exception

*/

public static String getMACAddress() {

// windows

String mac = getWindowsMACAddress();

// windows7

if (isNull(mac)) {

mac = getWindows7MACAddress();

}

// unix

if (isNull(mac)) {

mac = getUnixMACAddress();

}

if (!isNull(mac)) {

mac = mac.replace("-", "");

} else {

mac = "ABCDEFGHIJ";

}

return mac;

}

public static boolean isNull(Object strData) {

if (strData == null || String.valueOf(strData).trim().equals("")) {

return true;

}

return false;

}

public static void main(String[] args) {

System.out.println("当前操作系统:"+getOSName());

System.out.println(getWindows7MACAddress());

System.out.println(getMACAddress());

System.out.println(getWindowsMACAddress());

System.out.println(getUnixMACAddress());

}

}

java获取网卡正真的mac_java获取网卡的mac地址相关推荐

  1. linux shell获取同一网段存活主机的IP和MAC地址

    结果会在桌面生成一个mac_list.txt #!/bin/bash #获取网络中所有主机的MAC地址,把MAC地址输出到一个文件中(mac-list.txt) ip=$(ifconfig|grep ...

  2. centos7 的网卡配置文件设置bond接口. 如何固定mac地址?

    近来发我设置的bond接口的mac地址不是固定的. 可能是由系统启动时.加入网卡的顺序不一定导至的. 这里列出配置. #cat ifcfg-bond0 BONDING_OPTS="mode= ...

  3. 修改VMware虚拟机网卡MAC地址的方法总结

    修改MAC地址有两种方法: 一.修改vmx配置文件 修改vmx配置文件的两种方法 1.用文本工具(比如记事本或vi)打开vmx配置文件,然后修改一下几行: ethernet0.generatedAdd ...

  4. 【ethtool】ethtool 网卡诊断、调整工具、网卡性能优化| 解决丢包严重

    目录 即看即用 详细信息 软件简介 安装 ethtool的使用 输出详解 其他指令 将 ethtool 设置永久保存 如何使用 ethtool 优化 Linux 虚拟机网卡性能 ethtool 解决网 ...

  5. linux修改网卡的mac地址

    linux在安装一些软件的时候可能会用到修改主机的mac地址的问题,在网卡配置文件/etc/network/interface中添加mac地址的方式我在修改重启机器后没有生效,所以采用其他方式 在这里 ...

  6. linux虚拟网卡修改mac地址,Win10秘笈:两种方式修改网卡物理地址(MAC)

    每台能够上网的电脑都有网卡,不管是有线还是无线,网卡本身都得有物理地址,也就是MAC(Media Access Control 或 Medium Access Control)地址.这个地址理论上是固 ...

  7. Mac电脑如何查看本机网卡mac地址

    一.简述 每一台电脑的网卡,都有一个唯一的mac地址的,如果你使用的是mac电脑,怎么查看这个网卡的mac地址?接下来,让我们来看一下. 二.方法和步骤 点击桌面上的启动台 找到系统信息 点击网络下的 ...

  8. 在Windows下和Linux下如何查看网卡MAC地址、以及修改MAC地址

    在Windows下 利用DOS命令打开,弹出命令窗口. 输入命令ipconfig/all,回车. 其中,物理地址为本机的MAC地址. 在Linux下 查看MAC地址的方法有好多种,下面给出四种常见的方 ...

  9. 网卡驱动如何设置组播MAC地址

    最近关注了一些IP组播的知识,IP的组播需要以太网的支持.在这边文章内我们就主要讨论以太网如果支持IP组播. 首先看当前的interface是否支持multi-cast,如下面的命令红色部分标注,则说 ...

最新文章

  1. Kdtree(K-dimension tree)学习
  2. 开源 免费 java CMS - FreeCMS1.2-功能说明-网上调查
  3. vba保存文件为xlsx格式_Vba把Excel某个范围保存为XLS工作薄文件
  4. 便利的开发工具 CppUnit 快速使用指南
  5. Kubernetes文章汇总
  6. Linux 下的文件管理管理系统中的输出输入
  7. Linux有趣指令(一)
  8. fox pro删除单条数据_Mac文件夹数据同步工具——Sync Folders Pro
  9. 需要知道的面向对象设计的基本原则
  10. $GOPATH not set 问题解决方案
  11. WGAN-GP方法介绍
  12. Eclipse svn插件安装详细教程
  13. 网易严选(html+css+js)
  14. Linux-ubuntu系统查看显卡型号、显卡信息详解、显卡天梯图
  15. Android 360开源全面插件化框架RePlugin 实战
  16. python字母对照表_Python获取字母表
  17. ODrive应用 #3 odrive_gui入门指南
  18. Visual Studio 2017 C# 对 AutoCad2014 的二次开发设置
  19. vivo手机删除自带程序方法
  20. 松下A6伺服驱动器MADLN15SE与欧姆龙PLC的连接和试运行

热门文章

  1. python canvas画弧度_超清字符画——Python代码
  2. Tomcat的下载及环境变量配置
  3. SSM-jsp页面放在web-INF下受保护,读取出现404页面tomcat获取不到资源-问题解决配置docBase
  4. vue 日历 vue-calendar
  5. java web filter 入口_springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径
  6. 3维两点间的距离 js_高考必刷题4:球面上任意两点间距离的计算
  7. arp扫描工具_ARP扫描与ARP欺骗--Python的Scapy/Kamene模块学习之路
  8. linux管道和tee命令
  9. MySQL中的主键约束和外键约束
  10. MySql学习笔记——存储函数