JAVA获取MAC地址 JAVA获取本地MAC地址的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JAVA获取MAC地址 JAVA获取本地MAC地址的方法

一可?   2021-01-28 我要评论

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地址时从缓存中取,提高效率。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们