jva 获取本机正在使用的网卡Mac地址

package com.common;

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

import org.apache.log4j.Logger;

public class getMacAddress {
	private Logger logger=Logger.getLogger(this.getClass()); 

  
    public String getMacStr() throws Exception{  
    	InetAddress ia = InetAddress.getLocalHost();
		return getLocalMac(ia);		
    }  
  
    private static String getLocalMac(InetAddress ia) throws SocketException {
		// TODO Auto-generated method stub
		//获取网卡,获取地址
    	System.out.println(ia);
		byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
		System.out.println(mac);
		StringBuffer sb = new StringBuffer("");
		for(int i=0; i
以上代码可在有网络的环境下正常使用,如果无网络,请参考 另一篇

你可能感兴趣的:(Java)