FileUploadUtils.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.ruoyi.common.utils.file;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Objects;
  5. import org.apache.commons.io.FilenameUtils;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import com.ruoyi.common.config.RuoYiConfig;
  8. import com.ruoyi.common.constant.Constants;
  9. import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
  10. import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
  11. import com.ruoyi.common.exception.file.InvalidExtensionException;
  12. import com.ruoyi.common.utils.DateUtils;
  13. import com.ruoyi.common.utils.StringUtils;
  14. import com.ruoyi.common.utils.uuid.IdUtils;
  15. /**
  16. * 文件上传工具类
  17. *
  18. * @author ruoyi
  19. */
  20. public class FileUploadUtils
  21. {
  22. /**
  23. * 默认大小 50M
  24. */
  25. public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
  26. /**
  27. * 默认的文件名最大长度 100
  28. */
  29. public static final int DEFAULT_FILE_NAME_LENGTH = 100;
  30. /**
  31. * 默认上传的地址
  32. */
  33. private static String defaultBaseDir = RuoYiConfig.getProfile();
  34. public static void setDefaultBaseDir(String defaultBaseDir)
  35. {
  36. FileUploadUtils.defaultBaseDir = defaultBaseDir;
  37. }
  38. public static String getDefaultBaseDir()
  39. {
  40. return defaultBaseDir;
  41. }
  42. /**
  43. * 以默认配置进行文件上传
  44. *
  45. * @param file 上传的文件
  46. * @return 文件名称
  47. * @throws Exception
  48. */
  49. public static final String upload(MultipartFile file) throws IOException
  50. {
  51. try
  52. {
  53. return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
  54. }
  55. catch (Exception e)
  56. {
  57. throw new IOException(e.getMessage(), e);
  58. }
  59. }
  60. /**
  61. * 根据文件路径上传
  62. *
  63. * @param baseDir 相对应用的基目录
  64. * @param file 上传的文件
  65. * @return 文件名称
  66. * @throws IOException
  67. */
  68. public static final String upload(String baseDir, MultipartFile file) throws IOException
  69. {
  70. try
  71. {
  72. return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
  73. }
  74. catch (Exception e)
  75. {
  76. throw new IOException(e.getMessage(), e);
  77. }
  78. }
  79. /**
  80. * 文件上传
  81. *
  82. * @param baseDir 相对应用的基目录
  83. * @param file 上传的文件
  84. * @param allowedExtension 上传文件类型
  85. * @return 返回上传成功的文件名
  86. * @throws FileSizeLimitExceededException 如果超出最大大小
  87. * @throws FileNameLengthLimitExceededException 文件名太长
  88. * @throws IOException 比如读写文件出错时
  89. * @throws InvalidExtensionException 文件校验异常
  90. */
  91. public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
  92. throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
  93. InvalidExtensionException
  94. {
  95. int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
  96. if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
  97. {
  98. throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
  99. }
  100. assertAllowed(file, allowedExtension);
  101. String fileName = extractFilename(file);
  102. File desc = getAbsoluteFile(baseDir, fileName);
  103. file.transferTo(desc);
  104. return getPathFileName(baseDir, fileName);
  105. }
  106. /**
  107. * 编码文件名
  108. */
  109. public static final String extractFilename(MultipartFile file)
  110. {
  111. String fileName = file.getOriginalFilename();
  112. String extension = getExtension(file);
  113. fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
  114. return fileName;
  115. }
  116. public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
  117. {
  118. File desc = new File(uploadDir + File.separator + fileName);
  119. if (!desc.exists())
  120. {
  121. if (!desc.getParentFile().exists())
  122. {
  123. desc.getParentFile().mkdirs();
  124. }
  125. }
  126. return desc;
  127. }
  128. public static final String getPathFileName(String uploadDir, String fileName) throws IOException
  129. {
  130. int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
  131. String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  132. return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  133. }
  134. /**
  135. * 文件大小校验
  136. *
  137. * @param file 上传的文件
  138. * @return
  139. * @throws FileSizeLimitExceededException 如果超出最大大小
  140. * @throws InvalidExtensionException
  141. */
  142. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  143. throws FileSizeLimitExceededException, InvalidExtensionException
  144. {
  145. long size = file.getSize();
  146. if (size > DEFAULT_MAX_SIZE)
  147. {
  148. throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
  149. }
  150. String fileName = file.getOriginalFilename();
  151. String extension = getExtension(file);
  152. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
  153. {
  154. if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
  155. {
  156. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  157. fileName);
  158. }
  159. else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
  160. {
  161. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  162. fileName);
  163. }
  164. else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
  165. {
  166. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  167. fileName);
  168. }
  169. else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
  170. {
  171. throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
  172. fileName);
  173. }
  174. else
  175. {
  176. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  177. }
  178. }
  179. }
  180. /**
  181. * 判断MIME类型是否是允许的MIME类型
  182. *
  183. * @param extension
  184. * @param allowedExtension
  185. * @return
  186. */
  187. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  188. {
  189. for (String str : allowedExtension)
  190. {
  191. if (str.equalsIgnoreCase(extension))
  192. {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. /**
  199. * 获取文件名的后缀
  200. *
  201. * @param file 表单文件
  202. * @return 后缀名
  203. */
  204. public static final String getExtension(MultipartFile file)
  205. {
  206. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  207. if (StringUtils.isEmpty(extension))
  208. {
  209. extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
  210. }
  211. return extension;
  212. }
  213. }