IPutils获取本机mac

package com.ctl.util;

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

/**
 * 
 * @author Administrator
 *
 */
public class IPutils {
	/**
	 * 获取mac地址
	 * @return  mac
	 * @throws SocketException
	 * @throws UnknownHostException
	 */
	private static String getLocalMac() throws SocketException, UnknownHostException {
		//获取网卡,获取地址
		byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
		//System.out.println("mac数组长度:"+mac.length);
		StringBuffer sb = new StringBuffer("");
		for(int i=0; i<mac.length; i++) {
			if(i!=0) {
				sb.append("-");
			}
			//字节转换为整数
			int temp = mac[i]&0xff;
			String str = Integer.toHexString(temp);
			//System.out.println("每8位:"+str);
			if(str.length()==1) {
				sb.append("0"+str);
			}else {
				sb.append(str);
			}
		}
		return sb.toString().toUpperCase();
	}
}

你可能感兴趣的:(IPutils获取本机mac)