久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java獲取mac地址的方法有哪些

165次閱讀
沒有評論

共計 1760 個字符,預計需要花費 5 分鐘才能閱讀完成。

在 Java 中,可以使用以下方法之一來獲取 MAC 地址:

  1. 使用 NetworkInterface 類:
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class MacAddressUtil {public static String getMacAddress() {
try {Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {NetworkInterface networkInterface = networkInterfaces.nextElement();
byte[] mac = networkInterface.getHardwareAddress();
if (mac != null) {StringBuilder stringBuilder = new StringBuilder();
for (byte b : mac) {stringBuilder.append(String.format("%02X:", b));
}
if (stringBuilder.length() > 0) {stringBuilder.deleteCharAt(stringBuilder.length() - 1);
}
return stringBuilder.toString();}
}
} catch (SocketException e) {e.printStackTrace();
}
return null;
}
public static void main(String[] args) {String macAddress = getMacAddress();
System.out.println(macAddress);
}
}
  1. 使用 InetAddress 類獲取本地主機的 IP 地址,然后使用 Process 類執行 arp -a 命令,解析命令輸出來獲取 MAC 地址:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class MacAddressUtil {public static String getMacAddress() {
String ipAddress = null;
try {InetAddress inetAddress = InetAddress.getLocalHost();
ipAddress = inetAddress.getHostAddress();} catch (UnknownHostException e) {e.printStackTrace();
}
if (ipAddress != null) {
try {Process process = Runtime.getRuntime().exec("arp -a");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {if (line.contains(ipAddress)) {int index = line.indexOf("at") + 3;
return line.substring(index);
}
}
} catch (IOException e) {e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {String macAddress = getMacAddress();
System.out.println(macAddress);
}
}

請注意,獲取 MAC 地址可能因操作系統和網絡環境而異。

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-21發表,共計1760字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 绥化市| 临城县| 尤溪县| 天等县| 固安县| 富源县| 长白| 开封市| 香格里拉县| 灌云县| 岱山县| 郓城县| 乌拉特后旗| 镇赉县| 太仆寺旗| 澎湖县| 漳州市| 庆云县| 宽甸| 秀山| 兴安县| 吴旗县| 海淀区| 佳木斯市| 五指山市| 麻阳| 桂平市| 佛学| 区。| 庄浪县| 罗江县| 灵寿县| 彩票| 巴彦县| 马尔康县| 宁海县| 青州市| 山阳县| 宁安市| 拜泉县| 阳东县|