117.info
人生若只如初见

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

?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("X:", 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?????????????????

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe3e3AzsLBwBfAFM.html

推荐文章

  • Java正则表达式的基本用法是什么

    Java正则表达式是一种强大的字符串匹配工具,用于匹配和操作字符串的模式。它可以用于搜索、替换、验证和提取字符串中的特定模式。
    Java正则表达式的基本用...

  • Java找不到或无法加载主类问题怎么解决

    Java找不到或无法加载主类问题通常是由于以下几个原因引起的: 类路径错误:请确保你的类路径(classpath)设置正确。可以使用命令java -cp 指定类路径来运行程序...

  • Java正则表达式的基本用法有哪些

    Java正则表达式的基本用法有以下几种: 匹配:使用matches()方法来判断一个字符串是否与正则表达式匹配。例如:String regex = "abc"; String str = "abcdef"; b...

  • java并发问题如何解决

    Java中的并发问题可以通过以下几种方式来解决: 使用同步机制:可以使用synchronized关键字或者Lock接口来对共享资源进行同步控制,保证在同一时间只有一个线程可...

  • 关闭ssl连接不上服务器怎么解决

    如果您无法通过SSL连接到服务器,可以尝试以下解决方法: 检查网络连接:确保您的网络连接正常工作,并且没有任何网络问题。您可以尝试通过访问其他网站或使用其...

  • 服务器怎么强制开启ssl

    要强制启用SSL(Secure Socket Layer),您需要执行以下步骤: 获取SSL证书:您需要向可信的证书颁发机构(CA)申请并获取SSL证书。证书包含公钥和其他验证信息,...

  • c语言中fwrite函数怎么使用

    在C语言中,fwrite函数用于将指定数量的数据块写入到文件中。
    函数原型如下:
    size_t fwrite(const void *ptr, size_t size, size_t count, FILE *str...

  • python怎么向文件中写入数据

    在Python中,可以通过以下几种方式向文件中写入数据: 使用open()函数以写入模式打开文件,并利用write()函数写入数据,最后通过close()函数关闭文件。示例代码如...