FileUploadUtils.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.getParentFile().exists())
  120. {
  121. desc.getParentFile().mkdirs();
  122. }
  123. if (!desc.exists())
  124. {
  125. desc.createNewFile();
  126. }
  127. return desc;
  128. }
  129. private static final String getPathFileName(String uploadDir, String fileName) throws IOException
  130. {
  131. int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
  132. String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  133. String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  134. return pathFileName;
  135. }
  136. /**
  137. * 文件大小校验
  138. *
  139. * @param file 上传的文件
  140. * @return
  141. * @throws FileSizeLimitExceededException 如果超出最大大小
  142. * @throws InvalidExtensionException
  143. */
  144. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  145. throws FileSizeLimitExceededException, InvalidExtensionException
  146. {
  147. long size = file.getSize();
  148. if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
  149. {
  150. throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
  151. }
  152. String fileName = file.getOriginalFilename();
  153. String extension = getExtension(file);
  154. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
  155. {
  156. if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
  157. {
  158. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  159. fileName);
  160. }
  161. else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
  162. {
  163. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  164. fileName);
  165. }
  166. else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
  167. {
  168. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  169. fileName);
  170. }
  171. else
  172. {
  173. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  174. }
  175. }
  176. }
  177. /**
  178. * 判断MIME类型是否是允许的MIME类型
  179. *
  180. * @param extension
  181. * @param allowedExtension
  182. * @return
  183. */
  184. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  185. {
  186. for (String str : allowedExtension)
  187. {
  188. if (str.equalsIgnoreCase(extension))
  189. {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. /**
  196. * 获取文件名的后缀
  197. *
  198. * @param file 表单文件
  199. * @return 后缀名
  200. */
  201. public static final String getExtension(MultipartFile file)
  202. {
  203. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  204. if (StringUtils.isEmpty(extension))
  205. {
  206. extension = MimeTypeUtils.getExtension(file.getContentType());
  207. }
  208. return extension;
  209. }
  210. }