FileUploadUtils.java 6.5 KB

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