FileUtils.java 3.1 KB

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