下面用到的 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;
@Slf4j public class SystemInfoUtils {
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()); }
public static int getCpuCount() { java.lang.management.OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); return operatingSystemMXBean.getAvailableProcessors(); }
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"; }
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; }
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; }
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;
public class SystemInfoConstant {
public static final String IPV4_LOOP_ADDRESS = "0.0.1.1";
public static final String IPV6_LOOP_ADDRESS = "0:0:0:0:0:0:0:1%lo0";
public static final String LOCAL_HOST = "127.0.0.1";
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;
public class ExceptionUtil {
private static final Integer ERROR_MSG_MAX_LENGTH = 2000;
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; } }
|