Jdk11获取系统信息
闲话少说,直接上代码,下面用到的 api 仅在 jdk11 上测试通过,其他 jdk 版本没试过
工具类 SystemInfoUtils.java
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
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
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;
}
}