欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 獲取本機的IP與MAC地址實現(xiàn)詳解

 更新時間:2016年09月23日 14:31:41   作者:Lekko.Li  
這篇文章主要介紹了Java 獲取本機的IP與MAC地址實現(xiàn)詳解的相關資料,需要的朋友可以參考下

 Java 獲取本機的IP與MAC地址

有些機器有許多虛擬的網(wǎng)卡,獲取IP地址時會出現(xiàn)一些意外,所以需要一些驗證:

// 獲取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()

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

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

    1、獲取當前機器的操作系統(tǒng)

 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訪問要使用的代理服務器的地址
    prop.setProperty("http.proxyHost", HTTP_PROXY_HOST);
    // 設置http訪問要使用的代理服務器的端口
    prop.setProperty("http.proxyPort", HTTP_PROXY_PORT);
    // 設置不需要通過代理服務器訪問的主機,可以使用*通配符,多個地址用|分隔
    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的實例,有需要的朋友可以參考下,謝謝大家對本站的支持!

相關文章

最新評論