java如何查询本机ip地址和mac地址

如题所述

// 获取mac地址
  public static String getMacAddress() {
    try {
      Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
      byte[] mac = null;
      while (allNetInterfaces.hasMoreElements()) {
        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
          continue;
        } else {
          mac = netInterface.getHardwareAddress();
          if (mac != null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
              sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            if (sb.length() > 0) {
              return sb.toString();
            }
          }
        }
      }
    } catch (Exception e) {
      _logger.error("MAC地址获取失败", e);
    }
    return "";
  }
 
  // 获取ip地址
  public static String getIpAddress() {
    try {
      Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
      InetAddress ip = null;
      while (allNetInterfaces.hasMoreElements()) {
        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
          continue;
        } else {
          Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
          while (addresses.hasMoreElements()) {
            ip = addresses.nextElement();
            if (ip != null && ip instanceof Inet4Address) {
              return ip.getHostAddress();
            }
          }
        }
      }
    } catch (Exception e) {
      _logger.error("IP地址获取失败", e);
    }
    return "";
  }

希望能帮助到你

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-06-29
Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

import java.net.*;

public class NetInfo {
public static void main(String[] args) {
new NetInfo().say();
}
public void say() {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); //计算机名称和IP
System.out.println(i.getHostName()); //名称
System.out.println(i.getHostAddress()); //只获得IP
}
catch(Exception e){e.printStackTrace();}
}
}

也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。本回答被网友采纳
第2个回答  2017-06-29
 public static void getConfig(){ 
  try{ 
   InetAddress address = InetAddress.getLocalHost(); 
   NetworkInterface ni = NetworkInterface.getByInetAddress(address); 
   //ni.getInetAddresses().nextElement().getAddress(); 
   byte[] mac = ni.getHardwareAddress(); 
   String sIP = address.getHostAddress(); 
   String sMAC = ""; 
   Formatter formatter = new Formatter(); 
   for (int i = 0; i < mac.length; i++) { 
    sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i], 
          (i < mac.length - 1) ? "-" : "").toString();
   } 
   System.out.println("IP:" + sIP); 
   System.out.println("MAC:" + sMAC); 
  }catch(Exception e){ 
   e.printStackTrace(); 
  } 
 }

相似回答