diff --git a/src/main/java/com/luna/common/os/SystemInfoUtil.java b/src/main/java/com/luna/common/os/SystemInfoUtil.java index a1081858..74f03c4a 100644 --- a/src/main/java/com/luna/common/os/SystemInfoUtil.java +++ b/src/main/java/com/luna/common/os/SystemInfoUtil.java @@ -7,11 +7,12 @@ import java.util.*; import java.util.stream.Collectors; -import com.google.common.collect.Lists; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomUtils; - import org.apache.commons.lang3.StringUtils; -import org.checkerframework.checker.nullness.qual.Nullable; + +import com.google.common.collect.Lists; + import oshi.SystemInfo; import oshi.software.os.OSProcess; @@ -55,11 +56,27 @@ public static String getIP() { return getLocalHost().getHostAddress(); } + public static String getIpv4() { + return Objects.requireNonNull(getAllIpv4()).stream().findFirst().orElse(null); + } + /** * 本级ip 过滤 回环地址、链路本地地址或多播地址 * * @return */ + public static List getAllIpv4() { + List allIpAddress = getAllIpAddress(); + if (CollectionUtils.isEmpty(allIpAddress)) { + return new ArrayList<>(); + } + return allIpAddress.stream().filter(e -> !e.contains(":")).collect(Collectors.toList()); + } + + /** + * 本级ip 过滤 回环地址、链路本地地址或多播地址 + * @return + */ public static String getNoLoopbackIP() { return getAddress().getHostAddress(); } @@ -74,7 +91,6 @@ public static List getAllIpAddress() { /** * 获取非回环网卡IP - * * @return */ public static InetAddress getAddress() {