FileUploadUtils.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.constant.Constants;
  7. import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
  8. import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
  9. import com.ruoyi.common.exception.file.InvalidExtensionException;
  10. import com.ruoyi.common.utils.DateUtils;
  11. import com.ruoyi.common.utils.IdUtils;
  12. import com.ruoyi.common.utils.StringUtils;
  13. import com.ruoyi.framework.config.RuoYiConfig;
  14. /**
  15. * 文件上传工具类
  16. *
  17. * @author ruoyi
  18. */
  19. public class FileUploadUtils
  20. {
  21. /**
  22. * 默认大小 500M
  23. */
  24. public static final long DEFAULT_MAX_SIZE = 500 * 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. * @return 返回上传成功的文件名
  84. * @throws FileSizeLimitExceededException 如果超出最大大小
  85. * @throws FileNameLengthLimitExceededException 文件名太长
  86. * @throws IOException 比如读写文件出错时
  87. * @throws InvalidExtensionException 文件校验异常
  88. */
  89. public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
  90. throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
  91. InvalidExtensionException
  92. {
  93. int fileNamelength = file.getOriginalFilename().length();
  94. if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
  95. {
  96. throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
  97. }
  98. assertAllowed(file, allowedExtension);
  99. String fileName = extractFilename(file);
  100. File desc = getAbsoluteFile(baseDir, fileName);
  101. file.transferTo(desc);
  102. String pathFileName = getPathFileName(baseDir, fileName);
  103. return pathFileName;
  104. }
  105. /**
  106. * 编码文件名
  107. */
  108. public static final String extractFilename(MultipartFile file)
  109. {
  110. // String fileName = file.getOriginalFilename();
  111. // String extension = getExtension(file);
  112. String fileName = DateUtils.dateTime() + "/" + IdUtils.fastUUID().substring(5) + "/" + file.getOriginalFilename();
  113. return fileName;
  114. }
  115. private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
  116. {
  117. File desc = new File(uploadDir + File.separator + fileName);
  118. if (!desc.getParentFile().exists())
  119. {
  120. desc.getParentFile().mkdirs();
  121. }
  122. if (!desc.exists())
  123. {
  124. desc.createNewFile();
  125. }
  126. return desc;
  127. }
  128. private 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
  171. {
  172. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  173. }
  174. }
  175. }
  176. /**
  177. * 判断MIME类型是否是允许的MIME类型
  178. *
  179. * @param extension
  180. * @param allowedExtension
  181. * @return
  182. */
  183. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  184. {
  185. for (String str : allowedExtension)
  186. {
  187. if (str.equalsIgnoreCase(extension))
  188. {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * 获取文件名的后缀
  196. *
  197. * @param file 表单文件
  198. * @return 后缀名
  199. */
  200. public static final String getExtension(MultipartFile file)
  201. {
  202. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  203. if (StringUtils.isEmpty(extension))
  204. {
  205. extension = MimeTypeUtils.getExtension(file.getContentType());
  206. }
  207. return extension;
  208. }
  209. }