FileUploadUtils.java 6.7 KB

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