Jdk11获取系统信息

下面用到的 api 仅在 jdk11 上测试通过,其他 jdk 版本没试过

工具类 SystemInfoUtils.java

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

import com.demo.constant.SystemInfoConstant;
import com.sun.management.OperatingSystemMXBean;
import lombok.extern.slf4j.Slf4j;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.file.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

/**
* @description: 系统信息工具类
*/

@Slf4j
public class SystemInfoUtils {

/**
* 获取本地IP地址
* @return 本机 ip,过滤了回环地址和 localhost
*/
public static List<String> getLocalIP() {
List<String> ipList = new ArrayList<>();
try {
// 获取本地所有网络接口
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
// 排除虚拟接口和未启用的接口
if (networkInterface.isVirtual() || !networkInterface.isUp()) {
continue;
}
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLinkLocalAddress()) {
ipList.add(inetAddress.getHostAddress());
}
}
}
} catch (Exception e) {
log.error("本机 IP 获取失败, 异常详情: " + ExceptionUtil.getErrorMessage(e));
}
return ipList.stream().filter(e -> !SystemInfoConstant.INVALID_IP_LIST.contains(e)).collect(Collectors.toList());
}

/**
* 获取CPU数量
* @return 逻辑处理器数量,物理核数 * 2
*/
public static int getCpuCount() {
// 此处有坑,OperatingSystemMXBean 存在于两个包:java.lang.management.OperatingSystemMXBean 和 com.sun.management.OperatingSystemMXBean
// 一定要找对包,不然有些方法找不到,太特么坑了
java.lang.management.OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
return operatingSystemMXBean.getAvailableProcessors();
}

/**
* 获取总内存大小
* @return 物理内存大小
*/
public static String getTotalPhysicalMemory() {
OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
long physicalMemorySize = osBean.getTotalPhysicalMemorySize();
double physicalMemoryGB = (double) physicalMemorySize / 1024 / 1024 / 1024;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
return decimalFormat.format(physicalMemoryGB) + "GB";
}

/**
* 获取磁盘总大小
* @return 磁盘总量
*/
public static String getDiskSizeTotal() {
String diskSize = null;
try {
Path rootDir = Paths.get("/");
FileStore store = Files.getFileStore(rootDir);
long totalSpace = store.getTotalSpace();
double totalGB = (double) totalSpace / 1024 / 1024 / 1024;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
diskSize = decimalFormat.format(totalGB + "GB");
} catch (Exception e) {
log.error("磁盘信息获取失败, 异常详情: {}", ExceptionUtil.getErrorMessage(e));
}
return diskSize;
}

/**
* 获取已使用磁盘大小
* @return 磁盘已使用量
*/
public static String getDiskSizeUsed() {
String diskSize = null;
try {
Path rootDir = Paths.get("/");
FileStore store = Files.getFileStore(rootDir);
long usableSpace = store.getUsableSpace();
double usableGB = (double) usableSpace / 1024 / 1024 / 1024;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
diskSize = decimalFormat.format(usableGB + "GB");
} catch (Exception e) {
log.error("磁盘信息获取失败, 异常详情: {}", ExceptionUtil.getErrorMessage(e));
}
return diskSize;
}

/**
* 获取可用磁盘大小
* @return 磁盘可使用量
*/
public static String getDiskSizeFree() {
String diskSize = null;
try {
Path rootDir = Paths.get("/");
FileStore store = Files.getFileStore(rootDir);
long freeSpace = store.getUnallocatedSpace();
double freeGB = (double) freeSpace / 1024 / 1024 / 1024;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
diskSize = decimalFormat.format(freeGB + "GB");
} catch (Exception e) {
log.error("磁盘信息获取失败, 异常详情: {}", ExceptionUtil.getErrorMessage(e));
}
return diskSize;
}
}

常量类 SystemInfoConstant.java

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
import java.util.List;

/**
* @description: 系统信息常量
*/
public class SystemInfoConstant {
/**
* ipv4 回环地址
*/
public static final String IPV4_LOOP_ADDRESS = "0.0.1.1";

/**
* ipv6 回环地址
*/
public static final String IPV6_LOOP_ADDRESS = "0:0:0:0:0:0:0:1%lo0";

/**
* 本机 IP
*/
public static final String LOCAL_HOST = "127.0.0.1";

/**
* 无效的 ip 地址列表,需要排除
*/
public static final List<String> INVALID_IP_LIST = List.of(IPV4_LOOP_ADDRESS, IPV6_LOOP_ADDRESS, LOCAL_HOST);

异常信息获取工具类 ExceptionUtil.java

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
import org.springframework.util.StringUtils;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
* @description: 异常工具类
*/
public class ExceptionUtil {
/**
* 异常信息允许的最大长度,超过这个长度会被截取
*/
private static final Integer ERROR_MSG_MAX_LENGTH = 2000;

/**
* 获取异常的堆栈信息
*
* @param e 异常对象
* @return 堆栈信息
*/
public static String getErrorMessage(Exception e) {
if (StringUtils.hasText(e.getMessage())) {
return e.getMessage();
}
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, Boolean.TRUE));
String message = sw.toString();
return message.length() > ERROR_MSG_MAX_LENGTH ? message.substring(0, ERROR_MSG_MAX_LENGTH) : message;
}
}