FileUploadUtils.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.ruoyi.common.utils.file;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.commons.io.FilenameUtils;
  5. import org.springframework.web.multipart.MultipartFile;
  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 com.ruoyi.common.utils.StringUtils;
  13. import com.ruoyi.common.utils.uuid.IdUtils;
  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. private static String defaultBaseDir = RuoYiConfig.getProfile();
  33. public static void setDefaultBaseDir(String defaultBaseDir)
  34. {
  35. FileUploadUtils.defaultBaseDir = defaultBaseDir;
  36. }
  37. public static String getDefaultBaseDir()
  38. {
  39. return defaultBaseDir;
  40. }
  41. /**
  42. * 以默认配置进行文件上传
  43. *
  44. * @param file 上传的文件
  45. * @return 文件名称
  46. * @throws Exception
  47. */
  48. public static final String upload(MultipartFile file) throws IOException
  49. {
  50. try
  51. {
  52. return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
  53. }
  54. catch (Exception e)
  55. {
  56. throw new IOException(e.getMessage(), e);
  57. }
  58. }
  59. /**
  60. * 根据文件路径上传
  61. *
  62. * @param baseDir 相对应用的基目录
  63. * @param file 上传的文件
  64. * @return 文件名称
  65. * @throws IOException
  66. */
  67. public static final String upload(String baseDir, MultipartFile file) throws IOException
  68. {
  69. try
  70. {
  71. return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
  72. }
  73. catch (Exception e)
  74. {
  75. throw new IOException(e.getMessage(), e);
  76. }
  77. }
  78. /**
  79. * 文件上传
  80. *
  81. * @param baseDir 相对应用的基目录
  82. * @param file 上传的文件
  83. * @param allowedExtension 上传文件类型
  84. * @return 返回上传成功的文件名
  85. * @throws FileSizeLimitExceededException 如果超出最大大小
  86. * @throws FileNameLengthLimitExceededException 文件名太长
  87. * @throws IOException 比如读写文件出错时
  88. * @throws InvalidExtensionException 文件校验异常
  89. */
  90. public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
  91. throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
  92. InvalidExtensionException
  93. {
  94. int fileNamelength = file.getOriginalFilename().length();
  95. if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
  96. {
  97. throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
  98. }
  99. assertAllowed(file, allowedExtension);
  100. String fileName = extractFilename(file);
  101. File desc = getAbsoluteFile(baseDir, fileName);
  102. file.transferTo(desc);
  103. String pathFileName = getPathFileName(baseDir, fileName);
  104. return pathFileName;
  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. String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  133. return pathFileName;
  134. }
  135. /**
  136. * 文件大小校验
  137. *
  138. * @param file 上传的文件
  139. * @return
  140. * @throws FileSizeLimitExceededException 如果超出最大大小
  141. * @throws InvalidExtensionException
  142. */
  143. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  144. throws FileSizeLimitExceededException, InvalidExtensionException
  145. {
  146. long size = file.getSize();
  147. if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
  148. {
  149. throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
  150. }
  151. String fileName = file.getOriginalFilename();
  152. String extension = getExtension(file);
  153. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
  154. {
  155. if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
  156. {
  157. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  158. fileName);
  159. }
  160. else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
  161. {
  162. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  163. fileName);
  164. }
  165. else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
  166. {
  167. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  168. fileName);
  169. }
  170. else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
  171. {
  172. throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
  173. fileName);
  174. }
  175. else
  176. {
  177. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  178. }
  179. }
  180. }
  181. /**
  182. * 判断MIME类型是否是允许的MIME类型
  183. *
  184. * @param extension
  185. * @param allowedExtension
  186. * @return
  187. */
  188. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  189. {
  190. for (String str : allowedExtension)
  191. {
  192. if (str.equalsIgnoreCase(extension))
  193. {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. /**
  200. * 获取文件名的后缀
  201. *
  202. * @param file 表单文件
  203. * @return 后缀名
  204. */
  205. public static final String getExtension(MultipartFile file)
  206. {
  207. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  208. if (StringUtils.isEmpty(extension))
  209. {
  210. extension = MimeTypeUtils.getExtension(file.getContentType());
  211. }
  212. return extension;
  213. }
  214. }