FileUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package org.springblade.common.utils;
  2. import lombok.SneakyThrows;
  3. import org.springblade.common.constant.CommonConstant;
  4. import org.springblade.common.vo.FileSize;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.text.DecimalFormat;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Objects;
  15. public class FileUtils {
  16. /**
  17. * 获取文件path
  18. *
  19. * @param file
  20. * @return
  21. * @throws IOException
  22. */
  23. public static File convert(MultipartFile file) throws IOException {
  24. File convertFile = new File(Objects.requireNonNull(Objects.requireNonNull(file.getOriginalFilename())));
  25. FileOutputStream fos = null;
  26. try {
  27. convertFile.createNewFile();
  28. fos = new FileOutputStream(convertFile);
  29. fos.write(file.getBytes());
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. } finally {
  33. assert fos != null;
  34. fos.close();
  35. }
  36. return convertFile;
  37. }
  38. //获取文件大小
  39. public static List<FileSize> getOssFileSize(List<String> fileList) {
  40. List<FileSize> reData = new ArrayList<>();
  41. if (fileList != null) {
  42. for (String fileUrl : fileList) {
  43. fileUrl = CommonUtil.replaceOssUrl(fileUrl);
  44. FileSize file = new FileSize();
  45. file.setFileUrl(fileUrl);
  46. try {
  47. URLConnection openConnection = new URL(fileUrl).openConnection();
  48. int contentLength = openConnection.getContentLength();
  49. Double fileLength = Double.parseDouble(contentLength + "");
  50. file.setFileSize(Math.ceil(fileLength / 1024));
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. reData.add(file);
  55. }
  56. }
  57. return reData;
  58. }
  59. //获取OSS文件总大小
  60. public static Long getOssFileSizeCount(List<String> fileList) {
  61. Long count = 0L;
  62. if (fileList != null) {
  63. for (String fileUrl : fileList) {
  64. try {
  65. fileUrl = CommonUtil.replaceOssUrl(fileUrl);
  66. URLConnection openConnection = new URL(fileUrl).openConnection();
  67. count += openConnection.getContentLength();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. return count;
  74. }
  75. /**
  76. * 根据字节返回文件大小
  77. */
  78. public static String formatSize(long fileS) {
  79. DecimalFormat df = new DecimalFormat("#.00");
  80. String fileSizeString = "";
  81. String wrongSize = "0B";
  82. if (fileS == 0) {
  83. return wrongSize;
  84. }
  85. if (fileS < 1024) {
  86. fileSizeString = df.format((double) fileS) + "B";
  87. } else if (fileS < 1048576) {
  88. fileSizeString = df.format((double) fileS / 1024) + "KB";
  89. } else if (fileS < 1073741824) {
  90. fileSizeString = df.format((double) fileS / 1048576) + "MB";
  91. } else {
  92. fileSizeString = df.format((double) fileS / 1073741824) + "GB";
  93. }
  94. return fileSizeString;
  95. }
  96. }