Tonfay
2014-07-09 14:02:24
[个人笔记]Java获取本机MAC地址
01 | //获取网卡,获取地址 |
02 | InetAddress ia = InetAddress.getLocalHost(); |
03 | System.out.println(ia); |
04 | byte [] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); |
05 |
06 | System.out.println( "mac数组长度:" +mac.length); |
07 |
08 | StringBuffer sb = new StringBuffer( "" ); |
09 |
10 | for ( int i= 0 ; i<mac.length; i++) { |
11 |
12 | if (i!= 0 ) { |
13 |
14 | sb.append( "-" ); |
15 |
16 | } |
17 |
18 | //字节转换为整数 |
19 |
20 | int temp = mac[i]& 0xff ; |
21 |
22 | String str = Integer.toHexString(temp); |
23 |
24 | System.out.println( "每8位:" +str); |
25 |
26 | if (str.length()== 1 ) { |
27 |
28 | sb.append( "0" +str); |
29 |
30 | } else { |
31 |
32 | sb.append(str); |
33 |
34 | } |
35 |
36 | } |
37 |
38 | System.out.println( "本机MAC地址:" +sb.toString().toUpperCase()); |
ps:管理请改为0牛币
猜你喜欢
请下载代码后再发表评论



wjh12345654321 LV14
2021年11月11日
pdd1234 LV2
2021年9月1日
jlxxfjc LV1
2021年9月1日
itcaizhe LV9
2021年7月26日
Hadoop_CPU LV6
2020年12月24日
jachyn LV6
2020年9月9日
邈话12123 LV9
2020年8月8日
Thomas杨
2020年3月8日
暂无贡献等级
wywtiao LV6
2019年12月3日
H15029260755 LV1
2019年11月30日