java获取电脑mac_Java怎么获取电脑的MAC地址

private static String macAddressStr = null;

private static String computerName = System.getenv().get("COMPUTERNAME");

private static final String[] windowsCommand = { "ipconfig", "/all" };

private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };

private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*", Pattern.CASE_INSENSITIVE);

private final static List getMacAddressList() throws IOException {

final ArrayList macAddressList = new ArrayList();

final String os = System.getProperty("os.name");

final String command[];

if (os.startsWith("Windows")) {

command = windowsCommand;

}

else if (os.startsWith("Linux")) {

command = linuxCommand;

}

else {

throw new IOException("Unknow operating system:" + os);

}

final Process process = Runtime.getRuntime().exec(command);

BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

for (String line = null ; (line = bufReader.readLine()) != null ;) {

Matcher matcher = macPattern.matcher(line);

if (matcher.matches()) {

macAddressList.add(matcher.group(1));

}

}

process.destroy();

bufReader.close();

return macAddressList;

}

public static String getMacAddress() {

if (macAddressStr == null || macAddressStr.equals("")) {

StringBuffer sb = new StringBuffer();

try {

List macList = getMacAddressList();

for (Iterator iter = macList.iterator() ; iter.hasNext() ;) {

String amac = iter.next();

if (!amac.equals("0000000000E0")) {

sb.append(amac);

break;

}

}

}

catch (IOException e) {

e.printStackTrace();

}

macAddressStr = sb.toString();

}

return macAddressStr;

}

public static String getComputerName() {

if (computerName == null || computerName.equals("")) {

computerName = System.getenv().get("COMPUTERNAME");

}

return computerName;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值