JAVA获取本机的MAC地址
2024-09-07 13:50:55
/**
* 获取本机的Mac地址
* @return
*/
public String getMac() {
InetAddress ia;
byte[] mac = null;
try {
// 获取本地IP对象
ia = InetAddress.getLocalHost();
// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
} catch (Exception e) {
e.printStackTrace();
}
// 下面代码是把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();
}
最新文章
- How Do Annotations Work in Java?--转
- Html_Img元素 设置图片与其他元素横排高度一致
- 浮动框控制及切换、banner随机数js
- emu1
- iOS 定制controller过渡动画 ViewController Custom Transition使用体会
- Android系统目录结构
- C++ Primer : 第十一章 : 关联容器之关联容器的迭代器和操作
- c++中ifstream读文件的问题(关于eof())
- BF-KMP 算法
- [iOS UI进阶 - 3.1] 触摸事件的传递
- HTML入门教程(全套)
- 最全SpringMVC具体演示样例实战教程
- Eclipse4.4.2手动安装Veloeclipse-2.0.8
- makefile知识点归纳的
- Centos7.0下将Python更新到Python2.7.13
- ArcGIS API for JavaScript 4.2学习笔记[29] 热点(密度)分析——以报警频率为例【使用Geoprocessor类】
- 2015 多校联赛 ——HDU5402(模拟)
- Python开发——利用正则表达式实现计算器算法
- linux 学习笔记 wc命令
- Installing Windows Features without Internet
热门文章
- Codeforces 702F - T-shirts(平衡树+势能分析)
- Codeforces 739D - Recover a functional graph(二分图匹配)
- 洛谷 P4749 - [CERC2017]Kitchen Knobs(差分转换+dp,思维题)
- R 语言实战-Part 3 笔记
- python-django111111111111
- 【Linux】非root安装Python3及其包管理
- WebRTC网页打开摄像头并录制视频
- 点击下拉选择触发事件【c#】
- Spring Boot 热启动插件
- 如何在 ASP.NET Core 中构建轻量级服务