FileUploadUtils.java 6.6 KB

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