• Java 获取本地ip网卡信息


    工具类

      public static Optional<Inet4Address> getLocalIp4Address() throws SocketException {
            final List<Inet4Address> inet4Addresses = getLocalIp4AddressFromNetworkInterface();
            if (inet4Addresses.size() != 1) {
                final Optional<Inet4Address> ipBySocketOpt = getIpBySocket();
                if (ipBySocketOpt.isPresent()) {
                    return ipBySocketOpt;
                } else {
                    return inet4Addresses.isEmpty() ? Optional.empty() : Optional.of(inet4Addresses.get(0));
                }
            }
            return Optional.of(inet4Addresses.get(0));
        }
    
        private static Optional<Inet4Address> getIpBySocket() throws SocketException {
            try (final DatagramSocket socket = new DatagramSocket()) {
                socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
                if (socket.getLocalAddress() instanceof Inet4Address) {
                    return Optional.of((Inet4Address) socket.getLocalAddress());
                }
            } catch (UnknownHostException networkInterfaces) {
                throw new RuntimeException(networkInterfaces);
            }
            return Optional.empty();
        }
    
    
        public static List<Inet4Address> getLocalIp4AddressFromNetworkInterface() throws SocketException {
            List<Inet4Address> addresses = new ArrayList<>(1);
    
            // 所有网络接口信息
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            if (Objects.isNull(networkInterfaces)) {
                return addresses;
            }
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                //滤回环网卡、点对点网卡、非活动网卡、虚拟网卡并要求网卡名字是eth或ens开头
                if (!isValidInterface(networkInterface)) {
                    continue;
                }
    
                // 所有网络接口的IP地址信息
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    // 判断是否是IPv4,并且内网地址并过滤回环地址.
                    if (isValidAddress(inetAddress)) {
                        addresses.add((Inet4Address) inetAddress);
                    }
                }
            }
            return addresses;
        }
    
        private static boolean isValidAddress(InetAddress address) {
            return address instanceof Inet4Address && address.isSiteLocalAddress() && !address.isLoopbackAddress();
        }
    
        /**
         * 过滤回环网卡、点对点网卡、非活动网卡、虚拟网卡并要求网卡名字是eth或ens开头
         *
         * @param ni 网卡
         * @return 如果满足要求则true,否则false
         */
        private static boolean isValidInterface(NetworkInterface ni) throws SocketException {
            return !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp() && !ni.isVirtual() && (ni.getName().startsWith("eth") || ni.getName().startsWith("ens"));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    获取本机ip

    getLocalIp4Address().get().getHostAddress()
    
    • 1
  • 相关阅读:
    那些年,我们一起追过的Python BUG
    Google | Google Kubernetes Engine 集群实战
    深入探究 C++ 编程中的资源泄漏问题
    Vue3属性绑定方式分析
    [附源码]计算机毕业设计基于Springboot在线教育系统
    户外景区亲子儿童剧本杀小程序小程序开发搭建
    fastapi_No.18_后台应用
    vscode 打开后 默认terminal power shell 报错 名为“.C”的驱动器不存在。
    java计算机毕业设计景区在线购票系统源码+mysql数据库+系统+lw文档+部署
    多线程常见锁的策略
  • 原文地址:https://blog.csdn.net/qq_42702751/article/details/134510281