Skip to content

Commit

Permalink
🔖 2.5.2 ip地址优化
Browse files Browse the repository at this point in the history
  • Loading branch information
lunasaw committed Nov 25, 2023
1 parent 93da57e commit 8e0ee72
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/main/java/com/luna/common/os/SystemInfoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> getAllIpv4() {
List<String> 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();
}
Expand All @@ -74,7 +91,6 @@ public static List<String> getAllIpAddress() {

/**
* 获取非回环网卡IP
*
* @return
*/
public static InetAddress getAddress() {
Expand Down

0 comments on commit 8e0ee72

Please sign in to comment.