加上文件剩余大小
This commit is contained in:
parent
34ca251c67
commit
3c7fe4c13f
@ -144,6 +144,7 @@ public class StorageSourceConvertImpl implements StorageSourceConvert {
|
||||
if(StringUtils.isNotBlank(value)){
|
||||
//空间使用率
|
||||
storageSource.setSpaceOccupancyRatio(calculateLocalStorageUsage(value));
|
||||
storageSource.setRemainingSpaceSize(calculateFreeSpaceInGB(value));
|
||||
}
|
||||
|
||||
}else {
|
||||
@ -172,16 +173,20 @@ public class StorageSourceConvertImpl implements StorageSourceConvert {
|
||||
List<String> valueList = Arrays.asList(values);
|
||||
|
||||
StringBuilder spaceOccupancyRatio = new StringBuilder();
|
||||
StringBuilder remainingSpaceSize = new StringBuilder();
|
||||
|
||||
for (String valueData : valueList){
|
||||
String percentage = calculateLocalStorageUsage(valueData);
|
||||
String spaceSize = calculateFreeSpaceInGB(valueData);
|
||||
if (spaceOccupancyRatio.length() > 0) {
|
||||
spaceOccupancyRatio.append(";"); // 添加分号分隔
|
||||
}
|
||||
spaceOccupancyRatio.append(percentage);
|
||||
remainingSpaceSize.append(spaceSize);
|
||||
}
|
||||
//空间使用率
|
||||
storageSource.setSpaceOccupancyRatio(spaceOccupancyRatio.toString());
|
||||
storageSource.setRemainingSpaceSize(remainingSpaceSize.toString());
|
||||
}
|
||||
|
||||
|
||||
@ -239,6 +244,42 @@ public class StorageSourceConvertImpl implements StorageSourceConvert {
|
||||
throw new IllegalArgumentException("无法获取磁盘空间: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
//计算空间生育大小
|
||||
public static String calculateFreeSpaceInGB(String path) {
|
||||
try {
|
||||
Path target = Paths.get(path);
|
||||
|
||||
// 解析符号链接
|
||||
if (Files.isSymbolicLink(target)) {
|
||||
target = Files.readSymbolicLink(target);
|
||||
}
|
||||
|
||||
// 获取文件存储信息
|
||||
FileStore store = Files.getFileStore(target);
|
||||
long freeSpaceBytes = store.getUsableSpace();
|
||||
|
||||
// 转换为GB (1 GB = 1024^3 bytes)
|
||||
double freeSpaceGB = (double) freeSpaceBytes / (1024 * 1024 * 1024);
|
||||
|
||||
// 格式化输出
|
||||
DecimalFormat df = new DecimalFormat("#.##");
|
||||
String formatted = df.format(freeSpaceGB);
|
||||
|
||||
// 移除多余的小数位(如60.0 -> 60)
|
||||
if (formatted.endsWith(".00")) {
|
||||
formatted = formatted.substring(0, formatted.length() - 3);
|
||||
} else if (formatted.endsWith(".0")) {
|
||||
formatted = formatted.substring(0, formatted.length() - 2);
|
||||
}
|
||||
|
||||
return formatted + "GB";
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("无法获取磁盘空间: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 计算本地路径的空间使用率
|
||||
// public static String calculateLocalStorageUsage(String path) {
|
||||
//
|
||||
@ -385,6 +426,7 @@ public class StorageSourceConvertImpl implements StorageSourceConvert {
|
||||
storageSourceAdminResult.setStoreContent( storageSource.getStoreContent());
|
||||
storageSourceAdminResult.setSpaceOccupancyRatio( storageSource.getSpaceOccupancyRatio());
|
||||
storageSourceAdminResult.setValueData( storageSource.getValueData());
|
||||
storageSourceAdminResult.setRemainingSpaceSize( storageSource.getRemainingSpaceSize());
|
||||
return storageSourceAdminResult;
|
||||
}
|
||||
}
|
||||
|
@ -111,4 +111,8 @@ public class StorageSource implements Serializable {
|
||||
//存储路径
|
||||
@TableField(exist = false)
|
||||
private String valueData;
|
||||
|
||||
//存储剩余大小GB
|
||||
@TableField(exist = false)
|
||||
private String remainingSpaceSize;
|
||||
}
|
||||
|
@ -91,5 +91,7 @@ public class StorageSourceAdminResult {
|
||||
//存储路径
|
||||
@TableField(exist = false)
|
||||
private String valueData;
|
||||
|
||||
//存储剩余大小GB
|
||||
@TableField(exist = false)
|
||||
private String remainingSpaceSize;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user