获取mac地址的方法比较多,在这里介绍两种常用的方法,在Win9x 可用:WinIPcfg获得,在2000、XP可用IPconfig -all获得。如果你处于内部局域网内,可以用 nbtstat -A IP来获取指定ip地址计算机的mac地址,后者只能在2000/XP下使用。
代码如下:
package
test.io;
import java.io. * ;
/***/ /**
*获取本地和局域网中其他计算机MAC地址
*
*@author左杰
*
*/
public class MacTest ... {
/***//**
*获取本地mac地址,同多执行dos命令ipconfig-all
*
*@return
*/
publicstaticStringgetLocalMac()...{
StringMACAddr="";
try...{
Processprocess=Runtime.getRuntime().exec("ipconfig-all");
InputStreamReaderir=newInputStreamReader(process
.getInputStream());
LineNumberReaderinput=newLineNumberReader(ir);
Stringline;
while((line=input.readLine())!=null)...{
if(line.indexOf("PhysicalAddress")>0)...{
MACAddr=line.substring(line.indexOf("-")-2);
}
}
}catch(java.io.IOExceptione)...{
System.err.println("IOException"+e.getMessage());
}
returnMACAddr;
}
/***//**
*获取本地mac地址,同多执行dos命令nbtstat-Aip
*
*@paramip指定ip地址
*@return
*/
publicstaticStringgetMakeIpMac(Stringip)...{
StringMACAddr="";
try...{
if(ip.equals(""))...{
MACAddr="ip为空!";
}else...{
Processprocess=Runtime.getRuntime().exec("nbtstat-A"+ip);
InputStreamReaderir=newInputStreamReader(process
.getInputStream());
LineNumberReaderinput=newLineNumberReader(ir);
Stringline;
while((line=input.readLine())!=null)...{
if(line.indexOf("MACAddress")>0)...{
MACAddr=line.substring(line.indexOf("-")-2);
}
}
}
}catch(java.io.IOExceptione)...{
System.err.println("IOException"+e.getMessage());
}
returnMACAddr;
}
/***//**
*测试获取地址
*
*@paramargs
*/
publicstaticvoidmain(String[]args)...{
System.out.println("本地MAC地址为:"+getLocalMac());
Stringip="192.168.0.21";
System.out.println("ip地址为"+ip+"计算机的MAC地址为:"+getMakeIpMac(ip));
}
}
import java.io. * ;
/***/ /**
*获取本地和局域网中其他计算机MAC地址
*
*@author左杰
*
*/
public class MacTest ... {
/***//**
*获取本地mac地址,同多执行dos命令ipconfig-all
*
*@return
*/
publicstaticStringgetLocalMac()...{
StringMACAddr="";
try...{
Processprocess=Runtime.getRuntime().exec("ipconfig-all");
InputStreamReaderir=newInputStreamReader(process
.getInputStream());
LineNumberReaderinput=newLineNumberReader(ir);
Stringline;
while((line=input.readLine())!=null)...{
if(line.indexOf("PhysicalAddress")>0)...{
MACAddr=line.substring(line.indexOf("-")-2);
}
}
}catch(java.io.IOExceptione)...{
System.err.println("IOException"+e.getMessage());
}
returnMACAddr;
}
/***//**
*获取本地mac地址,同多执行dos命令nbtstat-Aip
*
*@paramip指定ip地址
*@return
*/
publicstaticStringgetMakeIpMac(Stringip)...{
StringMACAddr="";
try...{
if(ip.equals(""))...{
MACAddr="ip为空!";
}else...{
Processprocess=Runtime.getRuntime().exec("nbtstat-A"+ip);
InputStreamReaderir=newInputStreamReader(process
.getInputStream());
LineNumberReaderinput=newLineNumberReader(ir);
Stringline;
while((line=input.readLine())!=null)...{
if(line.indexOf("MACAddress")>0)...{
MACAddr=line.substring(line.indexOf("-")-2);
}
}
}
}catch(java.io.IOExceptione)...{
System.err.println("IOException"+e.getMessage());
}
returnMACAddr;
}
/***//**
*测试获取地址
*
*@paramargs
*/
publicstaticvoidmain(String[]args)...{
System.out.println("本地MAC地址为:"+getLocalMac());
Stringip="192.168.0.21";
System.out.println("ip地址为"+ip+"计算机的MAC地址为:"+getMakeIpMac(ip));
}
}