FileUploadUtils.java 7.3 KB

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