Java 擷取原生IP與MAC地址實現詳解_java

來源:互聯網
上載者:User

 Java 擷取原生IP與MAC地址

有些機器有許多虛擬網卡,擷取IP地址時會出現一些意外,所以需要一些驗證:

// 擷取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 "";  }

  以上的代碼中

netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()

    能很好地把一些非物理網卡或無用網上過濾掉,然後再取網上的IPV4地址即可。

     說到這裡,還有一些常用的:

    1、擷取當前機器的作業系統

 public final static String WIN_OS = "WINDOWS";  public final static String MAC_OS = "MAC";  public final static String LINUX_OS = "LINUX";  public final static String OTHER_OS = "OTHER";  public static String getOS() {    if (SystemUtils.IS_OS_WINDOWS){      return WIN_OS;    }    if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX){      return MAC_OS;    }    if (SystemUtils.IS_OS_UNIX){      return LINUX_OS;    }    return OTHER_OS;  }

    2、設定HTTP訪問代理

/**   * 設定http代理   */  public static void setHttpProxy() {    Properties prop = System.getProperties();    // 設定http訪問要使用的Proxy 伺服器的地址    prop.setProperty("http.proxyHost", HTTP_PROXY_HOST);    // 設定http訪問要使用的Proxy 伺服器的連接埠    prop.setProperty("http.proxyPort", HTTP_PROXY_PORT);    // 設定不需要通過Proxy 伺服器訪問的主機,可以使用*萬用字元,多個地址用|分隔    prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN);  }    /**   * 移除http代理   */  public static void removeHttpProxy() {    Properties prop = System.getProperties();    prop.remove("http.proxyHost");    prop.remove("http.proxyPort");    prop.remove("http.nonProxyHosts");  }

    在應用啟動時,訪問HTTP請求前,設定好就行。當然,http.nonProxyHosts可以不用設定,表示所有的HTTP請求都走代理。

    至於HTTPS代理,類似可以這樣設定:

System.setProperty("https.proxyHost", "HTTP_PROXY_HOST");
System.setProperty("https.proxyPort", "HTTP_PROXY_PORT");

以上就是Java 擷取本機IP和 MAC的執行個體,有需要的朋友可以參考下,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.