package com.yjm.testmac;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.UnknownHostException;import java.util.Enumeration;public class MacTest {	public static void main(String[] args) {		// 获得本机全部的mac地址信息		test();		// 根据当前的IP地址信息对象获得在用的网卡信息		test1();	}	private static void test1() {		System.out.println("==========test1方法中运行start......");		try {			InetAddress ipInfo = InetAddress.getLocalHost();			// 输出IP地址内容			System.out.println(ipInfo.getHostAddress());			// 获得网卡内容字节数组			byte[] byAddress = NetworkInterface.getByInetAddress(ipInfo)					.getHardwareAddress();			// 输出数组长度			System.out.println(byAddress.length);			// 地址字符串容器			StringBuffer stringBuffer = new StringBuffer();			// 单个数组值进行转换			for (int i = 0; i < byAddress.length; i++) {				// 单个数组值 就是一个地址位数添加分隔符				if (i != 0) {					stringBuffer.append("-");				}				// 字节转换为16进制数				int number = byAddress[i] & 0xff;				String strAddr = Integer.toHexString(number);				System.out.println("转换后:=======================================" + strAddr);				// 当转换的值是 个位数时,在前面补0				if (strAddr.length() == 1) {					stringBuffer.append("0" + strAddr);				} else {					stringBuffer.append(strAddr);				}			}			// 输出转换后的mac地址			System.out.println("转换后的MacAddress为:"					+ stringBuffer.toString().toUpperCase());		} catch (UnknownHostException e) {			e.printStackTrace();		} catch (SocketException e) {			e.printStackTrace();		}		System.out.println("==========test1方法中运行over......");	}	private static void test() {		System.out.println("==========test方法中运行start......");		// 初始化 网卡信息容器		Enumeration
 allNetDevices = null; // 初始化一张网卡对象 NetworkInterface networkInterface = null; // 等到网卡的byte数组信息 byte[] networkbyte = null; // 盛放 封装的网卡信息字符串 StringBuffer stringBuffer=null; try { // 获取网卡信息容器 allNetDevices = NetworkInterface.getNetworkInterfaces(); while (allNetDevices.hasMoreElements()) { // 获取单张网卡对象 迭代输出网卡信息 networkInterface = allNetDevices.nextElement(); // 获得正在 运行的 网卡信息 if (networkInterface.isUp()) { //转换字节后用到的网卡地址容器 stringBuffer = new StringBuffer(); //网卡mac地址字节容器 networkbyte = networkInterface.getHardwareAddress(); //网卡名字 System.out.println("网卡 名字: "+networkInterface.getName());                    //网卡信息 System.out.println("网卡信息 : "+networkInterface.getDisplayName()); // 单个数组值进行转换 for (int i = 0; i < networkbyte.length; i++) { // 单个数组值 就是一个地址位数添加分隔符 if (i != 0) { stringBuffer.append("-"); } // 字节转换为16进制数 int number = networkbyte[i] & 0xff; String strAddr = Integer.toHexString(number); System.out.println("转换后:==================================" + strAddr); // 当转换的值是 个位数时,在前面补0 if (strAddr.length() == 1) { stringBuffer.append("0" + strAddr); } else { stringBuffer.append(strAddr); } } // 输出转换后的mac地址 System.out.println("转换后的MacAddress为:" + stringBuffer.toString().toUpperCase()); } } } catch (SocketException e) { e.printStackTrace(); } System.out.println("==========test方法中运行 over......"); }}

方法在Linux下只能得到127.0.0.1,只能在Windows下获取正确的ip地址。

输出结果:

==========test方法中运行start......

网卡 名字: lo

网卡信息 : Software Loopback Interface 1

转换后的MacAddress为:

网卡 名字: eth3

网卡信息 : Realtek PCIe GBE Family Controller

转换后:==================================14

转换后:==================================da

转换后:==================================e9

转换后:==================================2b

转换后:==================================38

转换后:==================================10

转换后的MacAddress为:14-DA-E9-2B-38-10

网卡 名字: net4

网卡信息 : Teredo Tunneling Pseudo-Interface

转换后:==================================0

转换后:==================================0

转换后:==================================0

转换后:==================================0

转换后:==================================0

转换后:==================================0

转换后:==================================0

转换后:==================================e0

转换后的MacAddress为:00-00-00-00-00-00-00-E0

==========test方法中运行 over......

==========test1方法中运行start......

192.168.101.243

6

转换后:=======================================14

转换后:=======================================da

转换后:=======================================e9

转换后:=======================================2b

转换后:=======================================38

转换后:=======================================10

转换后的MacAddress为:14-DA-E9-2B-38-10

==========test1方法中运行over......