JAVA工具类---MACUtil

import java.net.NetworkInterface;
import java.util.Enumeration;

public class MacUtil {
	private MacUtil() {
	}

	public static String getMacAddress() throws Exception {
		Enumeration ni = NetworkInterface.getNetworkInterfaces();

		while (ni.hasMoreElements()) {
			NetworkInterface netI = ni.nextElement();

			byte[] bytes = netI.getHardwareAddress();
			if (netI.isUp() && netI != null && bytes != null && bytes.length == 6) {
				StringBuffer sb = new StringBuffer();
				for (byte b : bytes) {
					sb.append(Integer.toHexString((b & 240) >> 4));
					sb.append(Integer.toHexString(b & 15));
					sb.append("-");
				}
				sb.deleteCharAt(sb.length() - 1);
				return sb.toString().toUpperCase();
			}
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(MacUtil.getMacAddress());
	}
}

你可能感兴趣的:(技术专栏,macutil)