简体   繁体   English

如何获得计算机的MAC地址?

[英]How can I get the MAC address of my computer?

I want to get my MAC address. 我想获取我的MAC地址。 I used following code to do that. 我用下面的代码来做到这一点。 But I get output as FF . 但是我得到的输出为FF It is not my mac address. 这不是我的mac地址。 xx:xx:xx:xx:xx:ff is my mac address. 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;
}

How can I get my MAC address? 如何获得我的MAC地址?

I found this: 我找到了这个:

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();

  }

 }
}

Output: 输出:

Current IP address : 192.168.1.22 Current MAC address : 00-26-B9-9B-61-BF 当前IP地址:192.168.1.22当前MAC地址:00-26-B9-9B-61-BF

There are 2 problems that I see. 我看到两个问题。

Firstly you are reassigning the value of macaddress . 首先,您要重新分配macaddress的值。 Meaning there will always be just the last byte from mac . 意味着永远只有mac的最后一个字节。 The second problem is that you are iterating over all the interfaces your machine has, so even if you changed 第二个问题是您要遍历计算机具有的所有接口,因此即使您更改了

macaddress = String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
macaddress += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");

to append, rather than assign, you would get a string with all the MAC addresses your machine has. 追加而不是分配,您将获得一个包含计算机具有的所有MAC地址的字符串。 Also you would have to initialize macaddress to an empty string (since you cannot append to null ). 另外,您还必须将macaddress初始化为一个空字符串(因为您不能追加到null )。

Since you are iterating over all interfaces, how about returning a Collection of rather than a single String ? 由于您要遍历所有接口,因此如何返回Collection而不是单个String呢? Also using StringBuilder may be more appropriate. 另外,使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM