package com.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MacAddress { 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 static List<String> getMacAddressList() throws IOException { ArrayList<String> macAddressList = new ArrayList<String>(); 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("Unknown operating system: " + os); } final Process process = Runtime.getRuntime().exec(command); // Discard the stderr new Thread() { @Override public void run() { try { InputStream errorStream = process.getErrorStream(); while (errorStream.read() != -1) { } errorStream.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); // Extract the MAC addresses from stdout BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); for (String line = null; (line = reader.readLine()) != null;) { Matcher matcher = macPattern.matcher(line); if (matcher.matches()) { //macAddressList.add(matcher.group(1)); macAddressList.add(matcher.group(1).replaceAll("[-:]", "")); } } reader.close(); return macAddressList; } public static String getMacAddress() { try { List<String> addressList = getMacAddressList(); if (addressList.isEmpty()) { return ""; } return addressList.get(0); } catch (IOException e) { e.printStackTrace(); return ""; } } public static List<String> getMacAddressList2() { List<String> addressList=new ArrayList<String>(); try { List<String> temp = getMacAddressList(); addressList=temp; } catch (IOException e) { e.printStackTrace(); } return addressList; } public static String[] getMacAddresses() { try { return getMacAddressList().toArray(new String[0]); } catch (IOException e) { e.printStackTrace(); return new String[0]; } } public static void main(String[] args) { MacAddress mac = new MacAddress(); System.out.println("本电脑的MAC地址为:"+mac.getMacAddress()); } }
Java 获取MAC
精选 转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
前端如何获取MAC地址
获取硬件mac地址
c++ mac地址 -
java获取当月最大日
java获取当月最大日
System java 字段 -
mac 获取 java home mac 获取 iOS更新
一般情况下我们制作苹果系统U盘都是在苹果电脑上通过AppleStore下载制作的,但是有些时候我们通过系统更新或苹果商店得到的系统一般都是当前最新的,如果找需要的版本很不容易找到。今天我们提供另一种方法,通过一个小APP软件可以直接从苹果官方获取到各个版本的操作系统,包括制作U盘的详细方法。下载macOS系统组件从装有macOS系统的机器上,直接从AppStore并下载所需的OS版本,然后进行安装
mac 获取 java home c获取当前程序的路径 c获取当前系统时间 系统试图将驱动器合并到合并驱动器上的目录 系统安装