当前位置: 移动技术网 > IT编程>开发语言>Java > Java 获取本机的IP与MAC地址实现详解

Java 获取本机的IP与MAC地址实现详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

嵌入式培训骗局,浙江大学郑强,绿化树苗

 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访问要使用的代理服务器的地址
    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的实例,有需要的朋友可以参考下,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网