当前位置:首页 » 编程语言 » java获取地址

java获取地址

发布时间: 2025-03-14 21:44:30

java如何获取mac地址

以windows举例。x0dx0a运行命迹中令" cmd ipconfig /all"就会出现以轿拍下结姿帆山果x0dx0a x0dx0aPhysical Address. . . . . . . . . : 20-CF-30-9A-60-EEx0dx0a。x0dx0ajava就能过这样的命令来获取。以下是示例。x0dx0ax0dx0aimport java.io.BufferedReader;x0dx0aimport java.io.IOException;x0dx0aimport java.io.InputStreamReader;x0dx0ax0dx0apublic class TestMacx0dx0a{x0dx0a public static void main(String[] args) {x0dx0a System.out.println("Operation System=" + getOsName());x0dx0a System.out.println("Mac Address=" + getMACAddress());x0dx0a System.out.println("通过ip获取mac"+getMACAddress("192.168.1.101"));x0dx0a }x0dx0ax0dx0a public static String getOsName() {x0dx0a String os = "";x0dx0a os = System.getProperty("os.name");x0dx0a return os;x0dx0a }x0dx0a x0dx0a public static String getMACAddress() {x0dx0a String address = "";x0dx0a String os = getOsName();x0dx0a if (os.startsWith("Windows")) {x0dx0a try {x0dx0a String command = "cmd.exe /c ipconfig /all";x0dx0a Process p = Runtime.getRuntime().exec(command);x0dx0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));x0dx0a String line;x0dx0a while ((line = br.readLine()) != null) {x0dx0a if (line.indexOf("Physical Address") > 0) {x0dx0a int index = line.indexOf(":");x0dx0a index += 2;x0dx0a address = line.substring(index);x0dx0a break;x0dx0a }x0dx0a }x0dx0a br.close();x0dx0a return address.trim();x0dx0a } catch (IOException e) {x0dx0a }x0dx0a } else if (os.startsWith("Linux")) {x0dx0a String command = "/bin/sh -c ifconfig -a";x0dx0a Process p;x0dx0a try {x0dx0a p = Runtime.getRuntime().exec(command);x0dx0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));x0dx0a String line;x0dx0a while ((line = br.readLine()) != null) {x0dx0a if (line.indexOf("HWaddr") > 0) {x0dx0a int index = line.indexOf("HWaddr") + "HWaddr".length();x0dx0a address = line.substring(index);x0dx0a break;x0dx0a }x0dx0a }x0dx0a br.close();x0dx0a } catch (IOException e) {x0dx0a }x0dx0a }x0dx0a address = address.trim();x0dx0a return address;x0dx0a }x0dx0ax0dx0apublic static String getMACAddress(String ipAddress) { x0dx0aString str = "", strMAC = "", macAddress = ""; x0dx0atry { x0dx0aProcess pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); x0dx0aInputStreamReader ir = new InputStreamReader(pp.getInputStream()); x0dx0aLineNumberReader input = new LineNumberReader(ir); x0dx0afor (int i = 1; i < 100; i++) { x0dx0astr = input.readLine(); x0dx0aif (str != null) { x0dx0aif (str.indexOf("MAC Address") > 1) { x0dx0astrMAC = str.substring(str.indexOf("MAC Address") + 14, x0dx0astr.length()); x0dx0abreak; x0dx0a} x0dx0a} x0dx0a} x0dx0a} catch (IOException ex) { x0dx0areturn "Can't Get MAC Address!"; x0dx0a} x0dx0a// x0dx0aif (strMAC.length() < 17) { x0dx0areturn "Error!"; x0dx0a} x0dx0ax0dx0amacAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) x0dx0a+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) x0dx0a+ ":" + strMAC.substring(12, 14) + ":" x0dx0a+ strMAC.substring(15, 17); x0dx0a// x0dx0areturn macAddress; x0dx0a} x0dx0a} x0dx0ax0dx0a剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过x0dx0a我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。

❷ java中怎么获取电脑的mac地址

importjava.net.InetAddress;

importjava.net.NetworkInterface;

importjava.net.SocketException;

importjava.net.UnknownHostException;


/*

*物理地址是48位,别和ipv6搞错了

*/

publicclassLOCALMAC{


/**

*@paramargs

*@throwsUnknownHostException

*@throwsSocketException

*/

publicstaticvoidmain(String[]args)throwsUnknownHostException,SocketException{

//TODOAuto-generatedmethodstub



//得到IP,输出PC-201309011313/122.206.73.83

InetAddressia=InetAddress.getLocalHost();

System.out.println(ia);

getLocalMac(ia);

}


privatestaticvoidgetLocalMac(InetAddressia)throwsSocketException{

//TODOAuto-generatedmethodstub

//获取网卡,获取地址

byte[]mac=NetworkInterface.getByInetAddress(ia).getHardwareAddress();

System.out.println("mac数组长度:"+mac.length);

StringBuffersb=newStringBuffer("");

for(inti=0;i<mac.length;i++){

if(i!=0){

sb.append("-");

}

//字节转换为整数

inttemp=mac[i]&0xff;

Stringstr=Integer.toHexString(temp);

System.out.println("每8位:"+str);

if(str.length()==1){

sb.append("0"+str);

}else{

sb.append(str);

}

}

System.out.println("本机MAC地址:"+sb.toString().toUpperCase());

}

}

热点内容
以前的安卓游戏哪里下载 发布:2025-05-01 09:50:10 浏览:589
局域网电脑数据怎么备份到服务器 发布:2025-05-01 09:45:15 浏览:881
mmn算法 发布:2025-05-01 09:30:05 浏览:450
视频服务器搭建海康 发布:2025-04-30 23:50:29 浏览:96
xp共享怎么设置密码 发布:2025-04-30 23:50:24 浏览:160
租用电脑主机搭建服务器 发布:2025-04-30 23:28:06 浏览:733
php子类调用父类方法 发布:2025-04-30 23:11:55 浏览:678
存档加密id 发布:2025-04-30 22:20:50 浏览:748
mac搭建php环境 发布:2025-04-30 21:58:13 浏览:141
雅迪电动车配置有哪些 发布:2025-04-30 21:38:27 浏览:162