This question already has an answer here:
这个问题在这里已有答案:
- How to convert byte size into human readable format in java? 20 answers
如何在java中将字节大小转换为人类可读的格式? 20个答案
I was wondering if anyone knew of a good way to format files sizes in Java/JSP/JSTL pages.
我想知道是否有人知道在Java / JSP / JSTL页面中格式化文件大小的好方法。
Is there a util class that with do this?
I've searched commons but found nothing. Any custom tags?
Does a library already exist for this?
有没有这样的工具类吗?我搜查了公地但没有发现任何东西。任何自定义标签?库已经存在吗?
Ideally I'd like it to behave like the -h switch on Unix's ls command
理想情况下,我希望它的行为类似于Unix的ls命令中的-h开关
34 -> 34
795 -> 795
2646 -> 2.6K
2705 -> 2.7K
4096 -> 4.0K
13588 -> 14K
28282471 -> 27M
28533748 -> 28M
34 - > 34 795 - > 795 2646 - > 2.6K 2705 - > 2.7K 4096 - > 4.0K 13588 - > 14K 28282471 - > 27M 28533748 - > 28M
2 个解决方案
#1
A quick google search returned me this from Appache hadoop project. Copying from there: (Apache License, Version 2.0):
一个快速谷歌搜索从Appache hadoop项目返回我。从那里复制:( Apache许可证,版本2.0):
private static DecimalFormat oneDecimal = new DecimalFormat("0.0");
/**
* Given an integer, return a string that is in an approximate, but human
* readable format.
* It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.
* @param number the number to format
* @return a human readable form of the integer
*/
public static String humanReadableInt(long number) {
long absNumber = Math.abs(number);
double result = number;
String suffix = "";
if (absNumber < 1024) {
// nothing
} else if (absNumber < 1024 * 1024) {
result = number / 1024.0;
suffix = "k";
} else if (absNumber < 1024 * 1024 * 1024) {
result = number / (1024.0 * 1024);
suffix = "m";
} else {
result = number / (1024.0 * 1024 * 1024);
suffix = "g";
}
return oneDecimal.format(result) + suffix;
}
It uses 1K = 1024, but you can adapt this if you prefer. You also need to handle the <1024 case with a different DecimalFormat.
它使用1K = 1024,但如果您愿意,可以调整它。您还需要使用不同的DecimalFormat处理<1024情况。
#2
You can use the commons-io FileUtils.byteCountToDisplaySize
methods. For a JSTL implementation you can add the following taglib function while having commons-io on your classpath:
您可以使用commons-io FileUtils.byteCountToDisplaySize方法。对于JSTL实现,您可以在类路径上使用commons-io时添加以下taglib函数:
<function>
<name>fileSize</name>
<function-class>org.apache.commons.io.FileUtils</function-class>
<function-signature>String byteCountToDisplaySize(long)</function-signature>
</function>
Now in your JSP you can do:
现在在您的JSP中,您可以:
<%@ taglib uri="/WEB-INF/FileSizeFormatter.tld" prefix="sz"%>
Some Size: ${sz:fileSize(1024)} <!-- 1 K -->
Some Size: ${sz:fileSize(10485760)} <!-- 10 MB -->
#1
A quick google search returned me this from Appache hadoop project. Copying from there: (Apache License, Version 2.0):
一个快速谷歌搜索从Appache hadoop项目返回我。从那里复制:( Apache许可证,版本2.0):
private static DecimalFormat oneDecimal = new DecimalFormat("0.0");
/**
* Given an integer, return a string that is in an approximate, but human
* readable format.
* It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.
* @param number the number to format
* @return a human readable form of the integer
*/
public static String humanReadableInt(long number) {
long absNumber = Math.abs(number);
double result = number;
String suffix = "";
if (absNumber < 1024) {
// nothing
} else if (absNumber < 1024 * 1024) {
result = number / 1024.0;
suffix = "k";
} else if (absNumber < 1024 * 1024 * 1024) {
result = number / (1024.0 * 1024);
suffix = "m";
} else {
result = number / (1024.0 * 1024 * 1024);
suffix = "g";
}
return oneDecimal.format(result) + suffix;
}
It uses 1K = 1024, but you can adapt this if you prefer. You also need to handle the <1024 case with a different DecimalFormat.
它使用1K = 1024,但如果您愿意,可以调整它。您还需要使用不同的DecimalFormat处理<1024情况。
#2
You can use the commons-io FileUtils.byteCountToDisplaySize
methods. For a JSTL implementation you can add the following taglib function while having commons-io on your classpath:
您可以使用commons-io FileUtils.byteCountToDisplaySize方法。对于JSTL实现,您可以在类路径上使用commons-io时添加以下taglib函数:
<function>
<name>fileSize</name>
<function-class>org.apache.commons.io.FileUtils</function-class>
<function-signature>String byteCountToDisplaySize(long)</function-signature>
</function>
Now in your JSP you can do:
现在在您的JSP中,您可以:
<%@ taglib uri="/WEB-INF/FileSizeFormatter.tld" prefix="sz"%>
Some Size: ${sz:fileSize(1024)} <!-- 1 K -->
Some Size: ${sz:fileSize(10485760)} <!-- 10 MB -->