FileUploadUtils.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.ruoyi.common.utils.file;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.Paths;
  5. import java.util.Objects;
  6. import org.apache.commons.io.FilenameUtils;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import com.ruoyi.common.config.RuoYiConfig;
  9. import com.ruoyi.common.constant.Constants;
  10. import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
  11. import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
  12. import com.ruoyi.common.exception.file.InvalidExtensionException;
  13. import com.ruoyi.common.utils.DateUtils;
  14. import com.ruoyi.common.utils.StringUtils;
  15. import com.ruoyi.common.utils.uuid.Seq;
  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 = Objects.requireNonNull(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. String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
  104. file.transferTo(Paths.get(absPath));
  105. return getPathFileName(baseDir, fileName);
  106. }
  107. /**
  108. * 编码文件名
  109. */
  110. public static final String extractFilename(MultipartFile file)
  111. {
  112. return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
  113. FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
  114. }
  115. public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
  116. {
  117. File desc = new File(uploadDir + File.separator + fileName);
  118. if (!desc.exists())
  119. {
  120. if (!desc.getParentFile().exists())
  121. {
  122. desc.getParentFile().mkdirs();
  123. }
  124. }
  125. return desc;
  126. }
  127. public static final String getPathFileName(String uploadDir, String fileName) throws IOException
  128. {
  129. int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
  130. String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  131. return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  132. }
  133. /**
  134. * 文件大小校验
  135. *
  136. * @param file 上传的文件
  137. * @return
  138. * @throws FileSizeLimitExceededException 如果超出最大大小
  139. * @throws InvalidExtensionException
  140. */
  141. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  142. throws FileSizeLimitExceededException, InvalidExtensionException
  143. {
  144. long size = file.getSize();
  145. if (size > DEFAULT_MAX_SIZE)
  146. {
  147. throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
  148. }
  149. String fileName = file.getOriginalFilename();
  150. String extension = getExtension(file);
  151. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
  152. {
  153. if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
  154. {
  155. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  156. fileName);
  157. }
  158. else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
  159. {
  160. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  161. fileName);
  162. }
  163. else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
  164. {
  165. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  166. fileName);
  167. }
  168. else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
  169. {
  170. throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
  171. fileName);
  172. }
  173. else
  174. {
  175. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  176. }
  177. }
  178. }
  179. /**
  180. * 判断MIME类型是否是允许的MIME类型
  181. *
  182. * @param extension
  183. * @param allowedExtension
  184. * @return
  185. */
  186. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  187. {
  188. for (String str : allowedExtension)
  189. {
  190. if (str.equalsIgnoreCase(extension))
  191. {
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * 获取文件名的后缀
  199. *
  200. * @param file 表单文件
  201. * @return 后缀名
  202. */
  203. public static final String getExtension(MultipartFile file)
  204. {
  205. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  206. if (StringUtils.isEmpty(extension))
  207. {
  208. extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
  209. }
  210. return extension;
  211. }
  212. }