FileUploadUtils.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Global;
  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.StringUtils;
  12. import com.ruoyi.common.utils.security.Md5Utils;
  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. private static String defaultBaseDir = Global.getProfile();
  32. private static int counter = 0;
  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 extension 上传文件类型
  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. return fileName;
  104. }
  105. /**
  106. * 编码文件名
  107. */
  108. public static final String extractFilename(MultipartFile file)
  109. {
  110. String filename = file.getOriginalFilename();
  111. String extension = getExtension(file);
  112. filename = DateUtils.datePath() + File.separator + encodingFilename(filename) + "." + extension;
  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. /**
  129. * 编码文件名
  130. */
  131. private static final String encodingFilename(String filename)
  132. {
  133. filename = filename.replace("_", " ");
  134. filename = Md5Utils.hash(filename + System.nanoTime() + counter++);
  135. return filename;
  136. }
  137. /**
  138. * 文件大小校验
  139. *
  140. * @param file 上传的文件
  141. * @return
  142. * @throws FileSizeLimitExceededException 如果超出最大大小
  143. * @throws InvalidExtensionException
  144. */
  145. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  146. throws FileSizeLimitExceededException, InvalidExtensionException
  147. {
  148. long size = file.getSize();
  149. if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
  150. {
  151. throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
  152. }
  153. String filename = file.getOriginalFilename();
  154. String extension = getExtension(file);
  155. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
  156. {
  157. if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
  158. {
  159. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  160. filename);
  161. }
  162. else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
  163. {
  164. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  165. filename);
  166. }
  167. else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
  168. {
  169. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  170. filename);
  171. }
  172. else
  173. {
  174. throw new InvalidExtensionException(allowedExtension, extension, filename);
  175. }
  176. }
  177. }
  178. /**
  179. * 判断MIME类型是否是允许的MIME类型
  180. *
  181. * @param extension
  182. * @param allowedExtension
  183. * @return
  184. */
  185. public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
  186. {
  187. for (String str : allowedExtension)
  188. {
  189. if (str.equalsIgnoreCase(extension))
  190. {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. /**
  197. * 获取文件名的后缀
  198. *
  199. * @param file 表单文件
  200. * @return 后缀名
  201. */
  202. public static final String getExtension(MultipartFile file)
  203. {
  204. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  205. if (StringUtils.isEmpty(extension))
  206. {
  207. extension = MimeTypeUtils.getExtension(file.getContentType());
  208. }
  209. return extension;
  210. }
  211. }