admin管理员组

文章数量:1539849

2024年1月12日发(作者:)

Java获取系统物理地址(mac)

本人由于项目的需要,因此在网上找了很久的物理地址获取方式,找是找到了,不过全都是能过调用 ,能过它去执行ipconfig/all的命令,这个可能做为入门级的使用会比较合适,如果想找这种模式的,你可以去其它地方看看(其实最主要的是,如果这个程序运行在win7上,最终读出来的全是乱码,根本无法使用)。

在这里我要板述的就是能过网络接口包的一些方法获取物理地址,我觉得这一种方式才能可以在程序中使用,希望能对你的项目有所帮助。

以下是我的java类的代码:

s;

kInterface;

ation;

/**

* 获取系统的物理地址

* @author cdk

*

*/

public class SystemAddress {

/**

* 测试主函数

* @paramargs

*/

public static void main(String[] args) {

(getMac());

}

/***

* 将10进制转为16进制(物理地址是16进制的)

* @parambuf

* @return

*/

protected static String toHex(byte buf) {

int n = buf>= 0 ? buf : 256 + buf;

String str = tring(n);

if(()<2)str="0"+str;

rCase();

}

/***

* 获取物理地址

* @return

*/

private static String getMac() {

try {

Enumeration el = workInterfaces();

while (eElements()) {

byte[] mac = ement().getHardwareAddress();

if (mac == null||<1)

continue;

StringBuilder builder = new StringBuilder();

for (byte b : mac) {

String temp=toHex(b);

(temp);

("-");

}

CharAt(() - 1);

ng();

}

}

catch (Exception exception) {

tackTrace();

}

return null;

}

}

看到这里是不是想很想试一把呢,下载此文吧,如果觉得一个金币贵了,那你就来我的空间吧!/342145365/blog/item/

本文标签: 物理地址获取项目方式使用