Java获取Mac地址及IP地址2014-12-24 03:28:51

( 1人已投票,[高质量] )
分享:
31.3K

源生Java获取Mac地址及IP地址的代码:

测试环境:windows linux unix

[注]:请勿用localhost 127.0.0.1测试

Java代码如下:

IpUtil.java

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
                                                                
public class IpUtil {
                                                                  
  private IpUtil(){}
public static String getLocalIP() { //get local Ip address
    String sIP = "";
    InetAddress ip = null;
    try {
      boolean bFindIP = false;
      Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
          .getNetworkInterfaces();
      while (netInterfaces.hasMoreElements()) {
        if (bFindIP) {
          break;
        }
        NetworkInterface ni = (NetworkInterface) netInterfaces
            .nextElement();
                                                                        
        Enumeration<InetAddress> ips = ni.getInetAddresses();
        while (ips.hasMoreElements()) {
          ip = (InetAddress) ips.nextElement();
          if (!ip.isLoopbackAddress()
              && ip.getHostAddress().matches(
                  "(\\d{1,3}\\.){3}\\d{1,3}")) {
            bFindIP = true;
            break;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (null != ip) {
      sIP = ip.getHostAddress();
    }
    return sIP;
  }
                                                                  
      
public static String getMacId() { //get Mac address
    String macId = "";
    InetAddress ip = null;
    NetworkInterface ni = null;
    try {
      boolean bFindIP = false;
      Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
          .getNetworkInterfaces();
      while (netInterfaces.hasMoreElements()) {
        if (bFindIP) {
          break;
        }
        ni = (NetworkInterface) netInterfaces
            .nextElement();
        // ni.getName
        // 遍历IP
        Enumeration<InetAddress> ips = ni.getInetAddresses();
        while (ips.hasMoreElements()) {
          ip = (InetAddress) ips.nextElement();
          if (!ip.isLoopbackAddress() // 不是127.0.0.1
              && ip.getHostAddress().matches(
                  "(\\d{1,3}\\.){3}\\d{1,3}")) {
            bFindIP = true;
            break;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (null != ip) {
      try {
        macId = getMacFromBytes(ni.getHardwareAddress());
      } catch (SocketException e) {
        e.printStackTrace();
      }
    }
    return macId;
  }
                                                                  
  private static String getMacFromBytes(byte[] bytes) {
    StringBuffer mac = new StringBuffer();
    byte currentByte;
    boolean first = false;
    for (byte b : bytes) {
      if (first) {
        mac.append("-");
      }
      currentByte = (byte) ((b & 240) >> 4);
      mac.append(Integer.toHexString(currentByte));
      currentByte = (byte) (b & 15);
      mac.append(Integer.toHexString(currentByte));
      first = true;
    }
    return mac.toString().toUpperCase();
  }
                                                                  
                                                                  
  public static void main(String[] args) throws SocketException {
    System.out.println(getLocalIP());
    System.out.println(getMacId());
  }
                                                                  
}






头像

snowcoal
  • java
  • mac
  • ip

本文标签:

javamacip

收藏到我的私密空间

标题:Java获取Mac地址及IP地址

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~