FileUploadUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 extension 上传文件类型
  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. * @return
  107. * @throws FileSizeLimitExceededException 如果超出最大大小
  108. * @throws InvalidExtensionException
  109. */
  110. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  111. throws FileSizeLimitExceededException, InvalidExtensionException
  112. {
  113. long size = file.getSize();
  114. if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
  115. {
  116. throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
  117. }
  118. String fileName = file.getOriginalFilename();
  119. String extension = getExtension(file);
  120. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
  121. {
  122. if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
  123. {
  124. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  125. fileName);
  126. }
  127. else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
  128. {
  129. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  130. fileName);
  131. }
  132. else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
  133. {
  134. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  135. fileName);
  136. }
  137. else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
  138. {
  139. throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
  140. fileName);
  141. }
  142. else
  143. {
  144. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  145. }
  146. }
  147. }
  148. /**
  149. * 判断MIME类型是否是允许的MIME类型
  150. *
  151. * @param extension
  152. * @param allowedExtension
  153. * @return
  154. */
  155. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  156. {
  157. for (String str : allowedExtension)
  158. {
  159. if (str.equalsIgnoreCase(extension))
  160. {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * 获取文件名的后缀
  168. *
  169. * @param file 表单文件
  170. * @return 后缀名
  171. */
  172. public static final String getExtension(MultipartFile file)
  173. {
  174. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  175. if (StringUtils.isEmpty(extension))
  176. {
  177. extension = MimeTypeUtils.getExtension(file.getContentType());
  178. }
  179. return extension;
  180. }
  181. }