|
|
@@ -1,9 +1,14 @@
|
|
|
package com.uas.erp.schedular.listen;
|
|
|
|
|
|
-import java.net.InetAddress;
|
|
|
-import java.net.NetworkInterface;
|
|
|
-import java.net.SocketException;
|
|
|
-import java.net.UnknownHostException;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* 获取mac地址工具类
|
|
|
@@ -12,32 +17,74 @@ import java.net.UnknownHostException;
|
|
|
*/
|
|
|
public class DiskUtil {
|
|
|
|
|
|
- public static String getMacAddress(){
|
|
|
- String macAddress = "";
|
|
|
- try {
|
|
|
- InetAddress ia = InetAddress.getLocalHost();
|
|
|
- // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
|
|
|
- byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
|
|
|
+ private static String macAddressStr = null;
|
|
|
+ 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);
|
|
|
|
|
|
- // 下面代码是把mac地址拼装成String
|
|
|
+ /**
|
|
|
+ * 测试用的main方法.
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static String getAllMacAddress() {
|
|
|
+ if (macAddressStr == null || macAddressStr.equals("")) {
|
|
|
+ // 存放多个网卡地址用,目前只取一个非0000000000E0隧道的值
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
-
|
|
|
- for (int i = 0; i < mac.length; i++) {
|
|
|
- if (i != 0) {
|
|
|
- sb.append("-");
|
|
|
+ try {
|
|
|
+ List<String> macList = getMacAddressList();
|
|
|
+ for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {
|
|
|
+ String amac = iter.next();
|
|
|
+ if (!amac.equals("0000000000E0")) {
|
|
|
+ sb.append(amac);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- // mac[i] & 0xFF 是为了把byte转化为正整数
|
|
|
- String s = Integer.toHexString(mac[i] & 0xFF);
|
|
|
- sb.append(s.length() == 1 ? 0 + s : s);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- // 把字符串所有小写字母改为大写成为正规的mac地址并返回
|
|
|
- macAddress = sb.toString().toUpperCase();
|
|
|
- } catch (SocketException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (UnknownHostException e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ macAddressStr = sb.toString();
|
|
|
+
|
|
|
}
|
|
|
- return macAddress;
|
|
|
+
|
|
|
+ return macAddressStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取多个网卡地址
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private final static List<String> getMacAddressList() throws IOException {
|
|
|
+ final 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("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).replaceAll("[-:]","-"));
|
|
|
+// macAddressList.add(matcher.group(1).replaceAll("[-:]",
|
|
|
+// ""));//去掉MAC中的“-”
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ process.destroy();
|
|
|
+ bufReader.close();
|
|
|
+ return macAddressList;
|
|
|
}
|
|
|
|
|
|
}
|