![](/img/trans.png)
[英]How can i get list of MAC address and IP address of computer through java?
[英]How can I get the MAC address of my computer?
我想獲取我的MAC地址。 我用下面的代碼來做到這一點。 但是我得到的輸出為FF
。 這不是我的mac地址。 xx:xx:xx:xx:xx:ff
是我的mac地址。
public String getMacAddress() {
Enumeration<NetworkInterface> networks = null;
String macaddress=null;
try {
networks = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
NetworkInterface inter;
while (networks.hasMoreElements()) {
inter = networks.nextElement();
byte[] mac = null;
try {
mac = inter.getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
}
if (mac != null) {
for (int i = 0; i < mac.length; i++) {
macaddress=String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
System.out.println("");
}
}
return macaddress;
}
如何獲得我的MAC地址?
我找到了這個:
package com.mkyong;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class App{
public static void main(String[] args){
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}catch (SocketException e){
e.printStackTrace();
}
}
}
輸出:
當前IP地址:192.168.1.22當前MAC地址:00-26-B9-9B-61-BF
我看到兩個問題。
首先,您要重新分配macaddress
的值。 意味着永遠只有mac
的最后一個字節。 第二個問題是您要遍歷計算機具有的所有接口,因此即使您更改了
macaddress = String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
macaddress += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
追加而不是分配,您將獲得一個包含計算機具有的所有MAC地址的字符串。 另外,您還必須將macaddress
初始化為一個空字符串(因為您不能追加到null
)。
由於您要遍歷所有接口,因此如何返回Collection
而不是單個String
呢? 另外,使用StringBuilder
可能更合適。
public List<String> getMacAddress() { // Change return type from String to List<String>
Enumeration<NetworkInterface> networks = null;
try {
networks = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
NetworkInterface inter;
List<String> addresses = new ArrayList<>(); // Create list to store all MAC addresses
while (networks.hasMoreElements()) {
inter = networks.nextElement();
byte[] mac = null;
try {
mac = inter.getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
}
if (mac != null) {
StringBuilder sb = new StringBuilder(); // Rather than appending with += use a String builder
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); // Use append on the string builder
}
addresses.add(sb.toString()); // Add MAC address to the Collection
}
}
return addresses; // Return collection rather than one String
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.