FileUploadUtils.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.ruoyi.common.utils;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.shiro.crypto.hash.Md5Hash;
  5. import org.apache.tomcat.util.http.fileupload.FileUploadBase.FileSizeLimitExceededException;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
  8. import com.ruoyi.framework.config.RuoYiConfig;
  9. /**
  10. * 文件上传工具类
  11. *
  12. * @author ruoyi
  13. */
  14. public class FileUploadUtils
  15. {
  16. // 默认大小 50M
  17. public static final long DEFAULT_MAX_SIZE = 52428800;
  18. // 默认上传的地址
  19. private static String defaultBaseDir = RuoYiConfig.getProfile();
  20. // 默认的文件名最大长度
  21. public static final int DEFAULT_FILE_NAME_LENGTH = 200;
  22. // 默认文件类型jpg
  23. public static final String IMAGE_JPG_EXTENSION = ".jpg";
  24. private static int counter = 0;
  25. public static void setDefaultBaseDir(String defaultBaseDir)
  26. {
  27. FileUploadUtils.defaultBaseDir = defaultBaseDir;
  28. }
  29. public static String getDefaultBaseDir()
  30. {
  31. return defaultBaseDir;
  32. }
  33. /**
  34. * 以默认配置进行文件上传
  35. *
  36. * @param file 上传的文件
  37. * @return 文件名称
  38. * @throws Exception
  39. */
  40. public static final String upload(MultipartFile file) throws IOException
  41. {
  42. try
  43. {
  44. return upload(getDefaultBaseDir(), file, FileUploadUtils.IMAGE_JPG_EXTENSION);
  45. }
  46. catch (Exception e)
  47. {
  48. throw new IOException(e);
  49. }
  50. }
  51. /**
  52. * 根据文件路径上传
  53. *
  54. * @param baseDir 相对应用的基目录
  55. * @param file 上传的文件
  56. * @return 文件名称
  57. * @throws IOException
  58. */
  59. public static final String upload(String baseDir, MultipartFile file) throws IOException
  60. {
  61. try
  62. {
  63. return upload(baseDir, file, FileUploadUtils.IMAGE_JPG_EXTENSION);
  64. }
  65. catch (Exception e)
  66. {
  67. throw new IOException(e);
  68. }
  69. }
  70. /**
  71. * 文件上传
  72. *
  73. * @param baseDir 相对应用的基目录
  74. * @param file 上传的文件
  75. * @param needDatePathAndRandomName 是否需要日期目录和随机文件名前缀
  76. * @param extension 上传文件类型
  77. * @return 返回上传成功的文件名
  78. * @throws FileSizeLimitExceededException 如果超出最大大小
  79. * @throws FileNameLengthLimitExceededException 文件名太长
  80. * @throws IOException 比如读写文件出错时
  81. */
  82. public static final String upload(String baseDir, MultipartFile file, String extension)
  83. throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
  84. {
  85. int fileNamelength = file.getOriginalFilename().length();
  86. if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
  87. {
  88. throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
  89. FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
  90. }
  91. assertAllowed(file);
  92. String fileName = encodingFilename(file.getOriginalFilename(), extension);
  93. File desc = getAbsoluteFile(baseDir, baseDir + fileName);
  94. file.transferTo(desc);
  95. return fileName;
  96. }
  97. private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException
  98. {
  99. File desc = new File(File.separator + filename);
  100. if (!desc.getParentFile().exists())
  101. {
  102. desc.getParentFile().mkdirs();
  103. }
  104. if (!desc.exists())
  105. {
  106. desc.createNewFile();
  107. }
  108. return desc;
  109. }
  110. /**
  111. * 编码文件名
  112. */
  113. private static final String encodingFilename(String filename, String extension)
  114. {
  115. filename = filename.replace("_", " ");
  116. filename = new Md5Hash(filename + System.nanoTime() + counter++).toHex().toString() + extension;
  117. return filename;
  118. }
  119. /**
  120. * 文件大小校验
  121. *
  122. * @param file 上传的文件
  123. * @return
  124. * @throws FileSizeLimitExceededException 如果超出最大大小
  125. */
  126. public static final void assertAllowed(MultipartFile file) throws FileSizeLimitExceededException
  127. {
  128. long size = file.getSize();
  129. if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
  130. {
  131. throw new FileSizeLimitExceededException("not allowed upload upload", size, DEFAULT_MAX_SIZE);
  132. }
  133. }
  134. }