FileUtil.java 816 B

123456789101112131415161718192021222324252627
  1. package com.hssx.centerdata.util;
  2. public class FileUtil {
  3. /**
  4. * 获取容易识别的文件大小,比如KB, MB, GB
  5. * @param size
  6. * @return
  7. */
  8. public static String getReadableFileSize(long size) {
  9. if (size < 1024) {//1K以内
  10. return size + "byte";
  11. } else if (size < 1024 * 1024) {//1M以内
  12. return String.format("%.1fKB", (size*1.0f/1024));
  13. } else if (size < 1024 * 1024 * 1024) {//1G以内
  14. return String.format("%.1fMB", (size*1.0f/1024/1024));
  15. } else {
  16. return String.format("%.1fGB", (size*1.0f/1024/1024/1024));
  17. }
  18. }
  19. public static void main(String[] args) {
  20. long l = 1024 * 1024 * 1 * 1024;
  21. System.out.println(getReadableFileSize(l));
  22. String substring = "/upload/af9db74095354187b5ee17bce73b6b82.jpg".substring("/upload/".length());
  23. System.out.println(substring);
  24. }
  25. }