Java获取本机Mac地址

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Date;
import java.util.Enumeration;


public class HardwareUtil {
	public static String getMac() {
		StringBuffer buf = new StringBuffer();
		try {
			Enumeration el = NetworkInterface
					.getNetworkInterfaces();
			InetAddress ip = null;
			while (el.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) el.nextElement();
				byte[] mac = ni.getHardwareAddress();
				if (mac == null || mac.length == 0)
					continue;
				Enumeration nii = ni.getInetAddresses();
				while (nii.hasMoreElements()) {
					ip = nii.nextElement();
					if (ip instanceof Inet6Address)
						continue;
					if (!ip.isReachable(3000))
						continue;
					for (byte b : mac) {
						buf.append(b + "-");
					}
					buf.append("&");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buf.toString();
	}
	
	public static void main(String[] arg){
		String mac = HardwareUtil.getMac();
		System.out.println("mac:"+mac);
	}
}


你可能感兴趣的:(Java开发)