647f8a38e72a049239b57186c9e755bbb6353a05.svn-base
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.thinkgem.jeesite.common.utils;
import java.text.DecimalFormat;
/**
* <p>
* 文件大小工具类.
* </p>
*
* @author poplar.yfyang
* @version 1.0 2013-01-02 12:50 PM
* @since JDK 1.5
*/
public class FileSizeHelper {
public static long ONE_KB = 1024;
public static long ONE_MB = ONE_KB * 1024;
public static long ONE_GB = ONE_MB * 1024;
public static long ONE_TB = ONE_GB * (long)1024;
public static long ONE_PB = ONE_TB * (long)1024;
public static String getHumanReadableFileSize(Long fileSize) {
if(fileSize == null) return null;
return getHumanReadableFileSize(fileSize.longValue());
}
public static String getHumanReadableFileSize(long fileSize) {
if(fileSize < 0) {
return String.valueOf(fileSize);
}
String result = getHumanReadableFileSize(fileSize, ONE_PB, "PB");
if(result != null) {
return result;
}
result = getHumanReadableFileSize(fileSize, ONE_TB, "TB");
if(result != null) {
return result;
}
result = getHumanReadableFileSize(fileSize, ONE_GB, "GB");
if(result != null) {
return result;
}
result = getHumanReadableFileSize(fileSize, ONE_MB, "MB");
if(result != null) {
return result;
}
result = getHumanReadableFileSize(fileSize, ONE_KB, "KB");
if(result != null) {
return result;
}
return String.valueOf(fileSize)+"B";
}
private static String getHumanReadableFileSize(long fileSize, long unit, String unitName) {
if(fileSize == 0) return "0";
if(fileSize / unit >= 1) {
double value = fileSize / (double)unit;
DecimalFormat df = new DecimalFormat("######.##"+unitName);
return df.format(value);
}
return null;
}
}