JAVA获取本地MAC地址

InetAddress对象

此类表示Internet协议(IP)地址。 
IP地址是由IP使用的32位或128位无符号数字,构建UDP和TCP协议的低级协议。 IP地址结构由定义RFC 790: Assigned Numbers , RFC 1918: Address Allocation for Private Internets , RFC 2365: Administratively Scoped IP Multicast和RFC 2373: IP Version 6 Addressing Architecture 。 InetAddress的一个实例由一个IP地址和可能的相应主机名组成(取决于它是用主机名构造还是已经完成了反向主机名解析)。 

获取本地InetAddress对象

NetworkInterface对象

此类表示由名称组成的网络接口和分配给此接口的IP地址列表。 用于标识组播组所在的本地接口。 接口通常由诸如“le0”的名称所知。 

获取mac地址的方法

代码

	/**
	 * @Title: getMACAddress
	 * @Description: 通过InetAddress对象获取MAC地址
	 * @param inetAddress
	 * @return
	 * @throws Exception String
	 * @author: wangyk
	 * @date: 2020年11月23日 上午10:24:42
	 * @version: 2.0.1
	 */
	private static String getMACAddress(InetAddress inetAddress) throws Exception {
		// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
		byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
		// 下面代码是把mac地址拼装成String
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < mac.length; i++) {
			if (i != 0) {
				sb.append("-");
			}
			// mac[i] & 0xFF 是为了把byte转化为正整数
			String s = Integer.toHexString(mac[i] & 0xFF);
			sb.append(s.length() == 1 ? 0 + s : s);
		}
		// 把字符串所有小写字母改为大写成为正规的mac地址并返回
		return sb.toString().toUpperCase();
	}

演示

/**  
 * @Title: Test.java
 * @Description: 测试获取本地ip
 * @author: wangyk
 * @date: 2020年11月23日 上午10:21:13
 * @version: 2.0.1
*/
package com.yike.datamigration;

import java.net.InetAddress;
import java.net.NetworkInterface;

/**
 * @Title: Test.java
 * @Description: 测试获取本地ip
 * @author: wangyk
 * @date: 2020年11月23日 上午10:21:13
 * @version: 2.0.1
 */
public class Test {

	/**
	 * @Title: main
	 * @Description: 程序的入口
	 * @param args
	 * @throws Exception void
	 * @author: wangyk
	 * @date: 2020年11月23日 上午10:25:25
	 * @version: 2.0.1
	 */
	public static void main(String[] args) throws Exception {
		// 获取本机的InetAddress对象
		InetAddress localHost = InetAddress.getLocalHost();
		// 记录开始时间
		long start = System.currentTimeMillis();
		// 测试获取100次的执行时间
		for (int i = 0; i < 100; i++) {
			String mac = getMACAddress(localHost);
			System.out.println(i + "	" + mac);
		}
		// 记录结束时间
		long end = System.currentTimeMillis();
		System.out.println("总耗时:	" + (end - start));
	}

	/**
	 * @Title: getMACAddress
	 * @Description: 通过InetAddress对象获取MAC地址
	 * @param inetAddress
	 * @return
	 * @throws Exception String
	 * @author: wangyk
	 * @date: 2020年11月23日 上午10:24:42
	 * @version: 2.0.1
	 */
	private static String getMACAddress(InetAddress inetAddress) throws Exception {
		// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
		byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
		// 下面代码是把mac地址拼装成String
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < mac.length; i++) {
			if (i != 0) {
				sb.append("-");
			}
			// mac[i] & 0xFF 是为了把byte转化为正整数
			String s = Integer.toHexString(mac[i] & 0xFF);
			sb.append(s.length() == 1 ? 0 + s : s);
		}
		// 把字符串所有小写字母改为大写成为正规的mac地址并返回
		return sb.toString().toUpperCase();
	}

}

运行结果:
运行结果

建议

从运行结果来看,java获取本地MAC地址还是挺慢的。因为MAC不会轻易改变,所以可以考虑在项目运行时获取一次MAC地址,然后存放到缓存中,用到MAC地址时从缓存中取,提高效率。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值