当前位置:首页 » 编程语言 » 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-03-15 02:27:36 浏览:979
怎么重启远程服务器 发布:2025-03-15 02:26:53 浏览:247
u盘加密狗复制克隆软件 发布:2025-03-15 02:20:53 浏览:482
能玩VR的电脑要什么配置 发布:2025-03-15 02:19:36 浏览:715
明日之后电脑配置如何提高 发布:2025-03-15 02:08:39 浏览:862
c阶乘算法 发布:2025-03-15 02:08:39 浏览:364
挂锁忘记密码有什么办法 发布:2025-03-15 02:04:45 浏览:408
sql模糊查找 发布:2025-03-15 02:04:06 浏览:754
服务器如何不让玩家搞破坏 发布:2025-03-15 02:04:02 浏览:58
nmake编译win32 发布:2025-03-15 01:58:27 浏览:165