FileUploadUtil.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.huaxu.common;
  2. import org.apache.commons.io.FilenameUtils;
  3. import org.apache.commons.lang.time.DateFormatUtils;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.Date;
  9. /**
  10. * @author wyy
  11. * @date 2020-10-23 09:54
  12. */
  13. public class FileUploadUtil {
  14. /**
  15. * 默认大小 50M
  16. */
  17. public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
  18. /**
  19. * 默认的文件名最大长度 100
  20. */
  21. public static final int DEFAULT_FILE_NAME_LENGTH = 100;
  22. /**
  23. * 默认上传的地址
  24. */
  25. private static String defaultBaseDir = "/";
  26. private static int counter = 0;
  27. /**
  28. * 根据文件路径上传
  29. *
  30. * @param baseDir 相对应用的基目录
  31. * @param file 上传的文件
  32. * @return 文件名称
  33. * @throws IOException
  34. */
  35. public static final String uploadWeb(String baseDir, MultipartFile file) throws IOException {
  36. try {
  37. return upload(baseDir, file, MimeType.DEFAULT_ALLOWED_EXTENSION);
  38. } catch (Exception e) {
  39. throw new IOException(e.getMessage(), e);
  40. }
  41. }
  42. /**
  43. * 获取文件名的后缀
  44. *
  45. * @param file 表单文件
  46. * @return 后缀名
  47. */
  48. public static final String getExtension(MultipartFile file) {
  49. String extension = FilenameUtils.getExtension(file.getOriginalFilename());
  50. if (ToolUtil.isEmpty(extension)) {
  51. extension = MimeType.getExtension(file.getContentType());
  52. }
  53. return extension;
  54. }
  55. /**
  56. * 判断MIME类型是否是允许的MIME类型
  57. *
  58. * @param extension
  59. * @param allowedExtension
  60. * @return
  61. */
  62. public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
  63. for (String str : allowedExtension) {
  64. if (str.equalsIgnoreCase(extension)) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * 文件大小校验
  72. *
  73. * @param file 上传的文件
  74. * @return
  75. * @throws InvalidExtensionException
  76. */
  77. public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
  78. throws InvalidExtensionException {
  79. long size = file.getSize();
  80. if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE) {
  81. throw new RxcException("10004", "文件太大");
  82. }
  83. String fileName = file.getOriginalFilename();
  84. String extension = getExtension(file);
  85. if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
  86. if (allowedExtension == MimeType.IMAGE_EXTENSION) {
  87. throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
  88. fileName);
  89. } else if (allowedExtension == MimeType.FLASH_EXTENSION) {
  90. throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
  91. fileName);
  92. } else if (allowedExtension == MimeType.MEDIA_EXTENSION) {
  93. throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
  94. fileName);
  95. } else {
  96. throw new InvalidExtensionException(allowedExtension, extension, fileName);
  97. }
  98. }
  99. }
  100. /**
  101. * 编码文件名
  102. */
  103. public static final String extractFilename(MultipartFile file) {
  104. String fileName = file.getOriginalFilename();
  105. String extension = getExtension(file);
  106. fileName = DateFormatUtils.format(new Date(), "yyyy/MM/dd") + File.separator + ToolUtil.encodingFilename(fileName) + "." + extension;
  107. return fileName;
  108. }
  109. private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
  110. File desc = new File(uploadDir + File.separator + fileName);
  111. if (!desc.getParentFile().exists()) {
  112. desc.getParentFile().mkdirs();
  113. }
  114. if (!desc.exists()) {
  115. desc.createNewFile();
  116. }
  117. return desc;
  118. }
  119. private static final String getPathFileName(String uploadDir, String fileName) throws IOException {
  120. // int dirLastIndex = uploadDir.lastIndexOf("/") + 1;
  121. //String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  122. String pathFileName = uploadDir + "/" + fileName;
  123. return ToolUtil.path(pathFileName);
  124. }
  125. /**
  126. * 文件上传
  127. *
  128. * @param baseDir 相对应用的基目录
  129. * @param file 上传的文件
  130. * @return 返回上传成功的文件名
  131. * @throws IOException 比如读写文件出错时
  132. */
  133. public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws RxcException, IOException, InvalidExtensionException {
  134. int fileNamelength = file.getOriginalFilename().length();
  135. if (fileNamelength > FileUploadUtil.DEFAULT_FILE_NAME_LENGTH) {
  136. throw new RxcException("10004", "文件太大");
  137. }
  138. assertAllowed(file, allowedExtension);
  139. String fileName = extractFilename(file);
  140. File desc = getAbsoluteFile(baseDir, fileName);
  141. file.transferTo(desc);
  142. String pathFileName = getPathFileName(baseDir, fileName);
  143. return pathFileName;
  144. }
  145. }