@BeforeClass public static void beforeClass() { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); byte[] mac = ni.getHardwareAddress(); StringBuilder sb = new StringBuilder(); if (!DataTools.isEmpty(mac)) { for (byte b : mac) { sb.append(toHexByte(b)); sb.append("-"); } if (sb.length() > 1) sb.deleteCharAt(sb.length() - 1); if (sb.length() > 1) list.add(sb.toString()); } } } catch (Exception e) { // list = null; } finally { netInterfaces = null; } // getMacAddress(); }
/** * <br> * Created on: 2013-8-21 上午10:59:29 */ private static void getMacAddress() { BufferedReader br = null; Process process = null; Pattern macPattern = null; Matcher macMatcher = null; try { if (Env.getPlatform() == Env.getOsWindows()) process = Runtime.getRuntime().exec("cmd /c ipconfig /all"); else if (Env.getPlatform() == Env.getOsUnix()) process = Runtime.getRuntime().exec("ifconfig"); br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; macPattern = Pattern.compile("([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}"); boolean result = false; while ((line = br.readLine()) != null) { if (DataTools.isEmpty(line)) continue; macMatcher = macPattern.matcher(line); result = macMatcher.find(); if (result) list.add(macMatcher.group(0)); } br.close(); br = null; macMatcher = null; macPattern = null; } catch (IOException e) { } finally { try { if (!DataTools.isEmpty(br)) { br.close(); br = null; } macMatcher = null; macPattern = null; } catch (IOException e) { } finally { br = null; macMatcher = null; macPattern = null; } } }