package com.yjz.microweb.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * 网络工具类
 * @author Tony.Lau
 * @createTime 2016-10-19 23:56:06
 * @since 0.9.0
 * @email coffeelifelau@163.com
 * @blog<a href="http://blog.csdn.net/coffeelifelau">刘涛的编程笔记</a>
 */
public class InetUtil {
	
	/**
	 * 获取本机mac地址（带"-"）
	 */
	public static String getLocalMacAddressWithDash(){
		try {
			InetAddress inetAddress = InetAddress.getLocalHost();
			byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < mac.length; i++) {
				if (i != 0) {
					sb.append("-");
				}
				String s = Integer.toHexString(mac[i] & 0xFF);
				sb.append(s.length() == 1 ? 0 + s : s);
			}
			return sb.toString().trim().toUpperCase();
		} catch (UnknownHostException | SocketException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取本机mac地址（不带"-"）
	 */
	public static String getLocalMacAddress(){
		try {
			InetAddress inetAddress = InetAddress.getLocalHost();
			byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
			StringBuilder sb = new StringBuilder();
			if(mac == null)
			{
			    return sb.toString();
			}
			for (int i = 0; i < mac.length; i++) {
				String s = Integer.toHexString(mac[i] & 0xFF);
				sb.append(s.length() == 1 ? 0 + s : s);
			}
			return sb.toString().trim().toUpperCase();
		} catch (UnknownHostException | SocketException e) {
			return "";
		}
	}

}

@@ -158,16 +158,46 @@
 ception;
+%0Aimport java.util.Enumeration;
 %0A%0A/**%0A *
@@ -1102,66 +1102,99 @@
 %7B%0A%09%09
-%09InetAddress inetAddress = InetAddress.getLocalHost
+    Enumeration%3CNetworkInterface%3E enumer =  NetworkInterface.getNetworkInterfaces
 ();%0A%09%09
-%09
+    
 byte
@@ -1206,76 +1206,234 @@
 c = 
-NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
+null;%0A%09%09    while(enumer.hasMoreElements())%0A%09%09    %7B%0A%09%09        NetworkInterface ni = enumer.nextElement();%0A%09%09        mac = ni.getHardwareAddress();%0A%09%09        if(mac != null)%0A%09%09        %7B%0A%09%09            break;%0A%09%09        %7D%0A%09%09    %7D%0A
 %0A%09%09%09
@@ -1530,16 +1530,20 @@
 );%0A%09%09%09%7D%0A
+%09%09%09%0A
 %09%09%09for (
@@ -1714,32 +1714,32 @@
 .toUpperCase();%0A
+
 %09%09%7D catch (Unkno
@@ -1733,39 +1733,16 @@
  catch (
-UnknownHostException %7C 
 SocketEx

fix bug