欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > SpringBoot中获取系统及硬件信息

SpringBoot中获取系统及硬件信息

2025/4/30 8:04:18 来源:https://blog.csdn.net/tdcqfyl/article/details/147607301  浏览:    关键词:SpringBoot中获取系统及硬件信息

今天给大家介绍下,如何在SpringBoot中获取系统及硬件信息,话不多说,直接上干货 🔮

一.、引入maven依赖包

<dependency><groupId>com.github.oshi</groupId><artifactId>oshi-core</artifactId><version>6.4.1</version>
</dependency>

二、编写工具类

import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;import java.net.InetAddress;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Properties;public class OSUtils {/*** 获取CPU使用率* @return*/public static String getCpuUsage() {CpuInfo cpuInfo = OshiUtil.getCpuInfo();double free = cpuInfo.getFree();DecimalFormat format = new DecimalFormat("#.00");return format.format(100D - free)+" %";}/*** 获取内存信息* @return*/public static JSONObject getMemoryInfo() {JSONObject memoryInfo = new JSONObject();memoryInfo.put("total", formatByte(OshiUtil.getMemory().getTotal()));memoryInfo.put("free", formatByte(OshiUtil.getMemory().getAvailable()));memoryInfo.put("used", formatByte(OshiUtil.getMemory().getTotal() - OshiUtil.getMemory().getAvailable()));memoryInfo.put("usage", new DecimalFormat("#.## %").format(1.0 - OshiUtil.getMemory().getAvailable() * 1.0 / OshiUtil.getMemory().getTotal()));return memoryInfo;}/*** 获取磁盘信息* @return*/public static JSONArray getDiskInfo() {JSONObject diskInfo = null;JSONArray diskInfoArr = new JSONArray();SystemInfo systemInfo = new SystemInfo();OperatingSystem operatingSystem = systemInfo.getOperatingSystem();FileSystem fileSystem = operatingSystem.getFileSystem();List<OSFileStore> fileStores = fileSystem.getFileStores();for (OSFileStore fileStore : fileStores) {diskInfo = new JSONObject();// 盘符路径diskInfo.put("mountName", fileStore.getMount());// 磁盘类型diskInfo.put("diskType", fileStore.getType());// 磁盘容量diskInfo.put("total", formatByte(fileStore.getTotalSpace()));// 磁盘剩余容量diskInfo.put("free", formatByte(fileStore.getUsableSpace()));// 磁盘已使用容量diskInfo.put("used", formatByte(fileStore.getTotalSpace() - fileStore.getUsableSpace()));if (fileStore.getTotalSpace() == 0) {// 磁盘容量使用率diskInfo.put("usage", 0);} else {diskInfo.put("usage",new DecimalFormat("#.## %").format((fileStore.getTotalSpace() - fileStore.getUsableSpace()) * 1.0 / fileStore.getTotalSpace()));}diskInfoArr.add(diskInfo);}return diskInfoArr;}/*** 获取系统信息* @return*/public static JSONObject getSysInfo() {JSONObject sysInfo = new JSONObject();try {Properties props = System.getProperties();// 操作系统名称sysInfo.put("osName", props.getProperty("os.name"));// 系统架构sysInfo.put("osArch", props.getProperty("os.arch"));// 服务器名称sysInfo.put("hostName", InetAddress.getLocalHost().getHostName());// 服务器IpsysInfo.put("hostAddress", InetAddress.getLocalHost().getHostAddress());} catch (Exception e) {e.printStackTrace();}return sysInfo;}/*** 获取Java虚拟机信息* @return*/public static JSONObject getJvmInfo() {JSONObject jvmInfo = new JSONObject();Properties props = System.getProperties();Runtime runtime = Runtime.getRuntime();long jvmMaxMemory = runtime.maxMemory();long jvmTotalMemoryByte = runtime.totalMemory();long jvmFreeMemoryByte = runtime.freeMemory();long jvmUsedMemoryByte = jvmTotalMemoryByte - jvmFreeMemoryByte;// jvm最大可申请内存jvmInfo.put("max", formatByte(jvmMaxMemory));// jvm总内存jvmInfo.put("total", formatByte(jvmTotalMemoryByte));// jvm剩余内存jvmInfo.put("free", formatByte(jvmFreeMemoryByte));// jvm已使用内存jvmInfo.put("used", formatByte(jvmUsedMemoryByte));// jvm内存使用率jvmInfo.put("usage", new DecimalFormat("#.## %").format((jvmUsedMemoryByte) * 1.0 / jvmTotalMemoryByte));// jdk路径jvmInfo.put("jdkHome", props.getProperty("java.home"));// jdk版本jvmInfo.put("jdkVersion", props.getProperty("java.version"));// 进程idjvmInfo.put("pid", props.getProperty("PID"));// 项目路径jvmInfo.put("projectDir", props.getProperty("user.dir"));// 时区jvmInfo.put("timeZone", props.getProperty("user.timezone"));// 账户名称jvmInfo.put("userName", props.getProperty("user.name"));return jvmInfo;}/*** 单位转换*/private static String formatByte(long byteNumber) {// 换算单位double FORMAT = 1024.0;double kbNumber = byteNumber / FORMAT;if (kbNumber < FORMAT) {return new DecimalFormat("#.## KB").format(kbNumber);}double mbNumber = kbNumber / FORMAT;if (mbNumber < FORMAT) {return new DecimalFormat("#.## MB").format(mbNumber);}double gbNumber = mbNumber / FORMAT;if (gbNumber < FORMAT) {return new DecimalFormat("#.## GB").format(gbNumber);}double tbNumber = gbNumber / FORMAT;return new DecimalFormat("#.## TB").format(tbNumber);}}

三、效果展示

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词