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中,避免异常的最好方法是编写健壮的代码并遵循一些最佳实践。以下是一些建议: 预期和处理异常:使用try-catch语句来捕获和处理可能发生的异常。确保处理...

  • java中异常类有哪些常见类型

    Java中的异常类主要分为两大类:受检异常(Checked Exceptions)和非受检异常(Unchecked Exceptions)。以下是具体的异常类型:
    受检异常(Checked Except...

  • java中异常类怎么捕获

    在Java中,我们使用try-catch语句来捕获异常。当程序执行过程中遇到异常时,Java运行时系统会抛出一个异常对象。你可以使用try块来包含可能引发异常的代码,然后...

  • 关闭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()函数关闭文件。示例代码如...