FileUploadUtils.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. private 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. if (!desc.getParentFile().exists()) {
  121. desc.getParentFile().mkdirs();
  122. }
  123. }
  124. return desc;
  125. }
  126. private static final String getPathFileName(String uploadDir, String fileName) throws IOException
  127. {
  128. int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
  129. String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  130. String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  131. return pathFileName;
  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 (DEFAULT_MAX_SIZE != -1 && 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
  169. {
  170. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  171. }
  172. }
  173. }
  174. /**
  175. * 判断MIME类型是否是允许的MIME类型
  176. *
  177. * @param extension
  178. * @param allowedExtension
  179. * @return
  180. */
  181. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  182. {
  183. for (String str : allowedExtension)
  184. {
  185. if (str.equalsIgnoreCase(extension))
  186. {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * 获取文件名的后缀
  194. *
  195. * @param file 表单文件
  196. * @return 后缀名
  197. */
  198. public static final String getExtension(MultipartFile file)
  199. {
  200. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  201. if (StringUtils.isEmpty(extension))
  202. {
  203. extension = MimeTypeUtils.getExtension(file.getContentType());
  204. }
  205. return extension;
  206. }
  207. }