java 获取本机mac_java 获取本地 mac 地址

1 packagecn.com.sinosoft.monitor.vo;2

3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.net.InetAddress;7 importjava.net.NetworkInterface;8 importjava.net.UnknownHostException;9 importjava.util.ArrayList;10 importjava.util.List;11 importjava.util.Map;12

13 /**

14 * @className: SystemTool15 * @description: 与系统相关的一些常用工具方法. 目前实现的有:获取MAC地址、IP地址、主机名16 *@author: 笑遍世界17 * @createTime: 2010-11-13 下午08:03:4418 */

19 public classTool {20

21 /**

22 * 获取当前操作系统名称.23 * return 操作系统名称 例如:windows xp,linux 等.24 */

25 private staticString getOSName() {26 return System.getProperty("os.name").toLowerCase();27 }28

29 /**

30 * 获取unix网卡的mac地址.31 * 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.32 *33 *@returnmac地址34 */

35 private staticString getUnixMACAddress() {36 String mac = null;37 BufferedReader bufferedReader = null;38 Process process;39 try{40 process = Runtime.getRuntime().exec("ifconfig eth0");//linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息

41 bufferedReader = new BufferedReader(newInputStreamReader(process42 .getInputStream()));43 String line;44 intindex;45 while ((line = bufferedReader.readLine()) != null) {46 index = line.toLowerCase().indexOf("hwaddr");//寻找标示字符串[hwaddr]

47 if (index >= 0) {//找到了

48 mac = line.substring(index + "hwaddr".length() + 1).trim();//取出mac地址并去除2边空格

49 break;50 }51 }52 } catch(IOException e) {53 e.printStackTrace();54 } finally{55 try{56 if (bufferedReader != null) {57 bufferedReader.close();58 }59 } catch(IOException e1) {60 e1.printStackTrace();61 }62 }63

64 returnmac;65 }66

67 /**

68 * 获取widnows网卡的mac地址.69 *70 *@returnmac地址71 */

72 private static ListgetWindowsMACAddress() {73 List macList = new ArrayList();74 String mac = null;75 BufferedReader bufferedReader = null;76 Process process;77 try{78 //windows下的命令,显示信息中包含有mac地址信息

79 process = Runtime.getRuntime().exec("ipconfig /all");80 bufferedReader = new BufferedReader(newInputStreamReader(process81 .getInputStream(),"GBK"));82 String line;83 intindex;84 while ((line = bufferedReader.readLine()) != null) {85 index = line.toLowerCase().indexOf("physical address");//寻找标示字符串[physical address]

86

87 if (index == -1) {88 index = line.indexOf("物理地址");89 }90 if (index >= 0) {//找到了

91 index = line.indexOf(":");//寻找":"的位置

92 if (index >= 0) {93 mac = line.substring(index + 1).trim();//取出mac地址并去除2边空格

94 }95 macList.add(mac);96 }97 }98

99 } catch(IOException e) {100 e.printStackTrace();101 } finally{102 try{103 if (bufferedReader != null) {104 bufferedReader.close();105 }106 } catch(IOException e1) {107 e1.printStackTrace();108 }109 }110

111 returnmacList;112 }113

114 /**

115 *@return本机主机名116 */

117 private staticString getHostName() {118 InetAddress ia = null;119 try{120 ia =InetAddress.getLocalHost();121 } catch(UnknownHostException e) {122 e.printStackTrace();123 }124 if (ia == null) {125 return "some error..";126 } else

127 returnia.getHostName();128 }129

130 /**

131 *@return本机IP 地址132 */

133 private staticString getIPAddress() {134 InetAddress ia = null;135 try{136 ia =InetAddress.getLocalHost();137 } catch(UnknownHostException e) {138 e.printStackTrace();139 }140 if (ia == null) {141 return "some error..";142 } else

143 returnia.getHostAddress();144 }145

146 /**

147 * 测试用的main方法.148 *149 *@paramargc 运行参数.150 */

151 public static voidmain(String[] argc) {152 String os =getOSName();153 System.out.println("OS Type:" +os);154 if (os.contains("windows")) {155 //本地是windows

156 List mac =getWindowsMACAddress();157 System.out.println("MAC Address:" +mac);158 } else{159 //本地是非windows系统 一般就是unix

160 String mac =getUnixMACAddress();161 System.out.println(mac);162 }163 System.out.println("HostName:" +getHostName());164 System.out.println("IPAddress:" +getIPAddress());165

166

167 //这个更简单

168 Map map =System.getenv();169 String userName = map.get("USERNAME");//获取用户名

170 String computerName = map.get("COMPUTERNAME");//获取计算机名

171 String userDomain = map.get("USERDOMAIN");//获取计算机域名

172

173

174 System.out.println(userName);175 System.out.println(computerName);176 System.out.println(userDomain);177

178 System.out.println(GetMac());179 }180

181 staticString GetMac(String... ip)182 {183 List macList = new ArrayList();184 InetAddress ia;185 byte[] mac = null;186 try{187 //获取本地IP对象

188 ia =InetAddress.getLocalHost();189 //获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

190 mac =NetworkInterface.getByInetAddress(ia).getHardwareAddress();191 } catch(Exception e) {192 e.printStackTrace();193 }194 //下面代码是把mac地址拼装成String

195 StringBuffer sb = newStringBuffer();196 for(int i=0;i

201 String s = Integer.toHexString(mac[i] & 0xFF);202 sb.append(s.length()==1?0+s:s);203 }204

205 //把字符串所有小写字母改为大写成为正规的mac地址并返回

206 returnsb.toString().toUpperCase();207 }208

209 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值