SpringBoot 文件大写转换
将文件大小(字节),达到KB的转成KB,达到M的转成M
/**
* 方法一
* @param size
* @return
*/
public static String toSize(long size) {
if (size <= 0) {
return "0";
}
final String[] units = new String[]{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,###.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
/**
* 方法二
* @param size
* @return
*/
// 定义单位常量
private static final long KB = 1024L;
private static final long MB = KB * 1024L;
private static final long GB = MB * 1024L;
private static final long TB = GB * 1024L;
public static String readableFileSize(long bytes) {
if (bytes >= TB) {
return String.format("%.2f TB", (double) bytes / TB);
} else if (bytes >= GB) {
return String.format("%.2f GB", (double) bytes / GB);
} else if (bytes >= MB) {
return String.format("%.2f MB", (double) bytes / MB);
} else if (bytes >= KB) {
return String.format("%.2f KB", (double) bytes / KB);
} else {
return String.format("%d B", bytes);
}
}
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/18570115
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!