From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/SysFileSysUtil.java | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 113 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/SysFileSysUtil.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/SysFileSysUtil.java
new file mode 100644
index 0000000..fc1976a
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/SysFileSysUtil.java
@@ -0,0 +1,113 @@
+package com.dy.common.util;
+
+import lombok.Data;
+import oshi.software.os.FileSystem;
+import oshi.software.os.OSFileStore;
+import oshi.software.os.OperatingSystem;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 13:54
+ * @Description
+ */
+public class SysFileSysUtil {
+ /**
+ * 纾佺洏淇℃伅
+ */
+ public static LinkedList<SysFile> info(OperatingSystem os) {
+ LinkedList<SysFile> sysFiles = new LinkedList<SysFile>();
+ FileSystem fileSystem = os.getFileSystem();
+ List<OSFileStore> fsArray = fileSystem.getFileStores();
+ for (OSFileStore fs : fsArray) {
+ long free = fs.getUsableSpace();
+ long total = fs.getTotalSpace();
+ long used = total - free;
+ SysFile sysFile = new SysFile();
+ sysFile.setDirName(fs.getMount());
+ sysFile.setSysTypeName(fs.getType());
+ sysFile.setTypeName(fs.getName());
+ sysFile.setTotal(convertFileSize(total));
+ sysFile.setFree(convertFileSize(free));
+ sysFile.setUsed(convertFileSize(used));
+ sysFile.setUsage(DoubleUtil.mul(DoubleUtil.div(used, total, 4), 100));
+ sysFiles.add(sysFile);
+ }
+ return sysFiles ;
+ }
+
+ /**
+ * 瀛楄妭杞崲
+ *
+ * @param size 瀛楄妭澶у皬
+ * @return 杞崲鍚庡��
+ */
+ public static String convertFileSize(long size) {
+ long kb = 1024;
+ long mb = kb * 1024;
+ long gb = mb * 1024;
+ if (size >= gb) {
+ return String.format("%.1f GB", (float) size / gb);
+ } else if (size >= mb) {
+ float f = (float) size / mb;
+ return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
+ } else if (size >= kb) {
+ float f = (float) size / kb;
+ return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
+ } else {
+ return String.format("%d B", size);
+ }
+ }
+ @Data
+ public static class SysFile{
+ /**
+ * 鐩樼璺緞
+ */
+ public String dirName;
+
+ /**
+ * 鐩樼绫诲瀷
+ */
+ public String sysTypeName;
+
+ /**
+ * 鏂囦欢绫诲瀷
+ */
+ public String typeName;
+
+ /**
+ * 鎬诲ぇ灏�
+ */
+ public String total;
+
+ /**
+ * 鍓╀綑澶у皬
+ */
+ public String free;
+
+ /**
+ * 宸茬粡浣跨敤閲�
+ */
+ public String used;
+
+ /**
+ * 璧勬簮鐨勪娇鐢ㄧ巼
+ */
+ public double usage;
+
+ public String toString(){
+ StringBuilder sb = new StringBuilder() ;
+ sb.append(" 纾佺洏" + dirName + "\n");
+ sb.append(" 鐩樼绫诲瀷锛�" + sysTypeName + "\n");
+ sb.append(" 鏂囦欢绫诲瀷锛�" + typeName + "\n");
+ sb.append(" 鎬诲ぇ灏忥細" + total + "\n");
+ sb.append(" 鍓╀綑澶у皬锛�" + free + "\n");
+ sb.append(" 宸茬粡浣跨敤閲忥細" + used + "\n");
+ sb.append(" 璧勬簮浣跨敤鐜囷細" + usage + "\n");
+
+ return sb.toString() ;
+ }
+ }
+}
--
Gitblit v1.8.0