• 技术文章 >Java >java教程

    java获取本机所有网卡的IP地址(ipv4)

    巴扎黑巴扎黑2016-12-28 17:55:05原创1294
    /**
    * 获取机器所有网卡的IP(ipv4)
    * @return
    */
    public static List<String> getLocalIP() {
    List<String> ipList = new ArrayList<String>();
    InetAddress ip = null;
    try {
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
    // 遍历所有ip
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    ip = (InetAddress) ips.nextElement();
    if (null == ip || "".equals(ip)) {
    continue;
    }
    String sIP = ip.getHostAddress();
    if(sIP == null || sIP.indexOf(":") > -1) {
    continue;
    }
    ipList.add(sIP);
    System.out.println(sIP);
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return ipList;
    }
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:JVM内存管理------垃圾搜集器参数精解 下一篇:List排序
    PHP编程就业班

    相关文章推荐

    • JAVA学习IO操作之字节流和字符流(总结分享)• 完全掌握JAVA流程控制• Java学习总结之数组(整理分享)• Java工厂方法模式详解• 详细整理java枚举的使用总结

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网