在线客服

FileUploadHelper.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package api.util.helper;
  2. import org.springframework.stereotype.Component;
  3. import org.springframework.web.multipart.MultipartFile;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.file.Paths;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.Objects;
  10. /**
  11. * @author Administrator
  12. */
  13. @Component
  14. public class FileUploadHelper {
  15. /**
  16. * 根据文件路径上传
  17. * @param baseDir 相对应用的基目录
  18. * @param file 上传的文件
  19. * @return 文件名称
  20. * @throws IOException
  21. */
  22. public static String upload(String baseDir, MultipartFile file) throws IOException
  23. {
  24. try
  25. {
  26. int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
  27. if (fileNamelength > 50)
  28. {
  29. throw new IOException("文件名不能超过50");
  30. }
  31. long size = file.getSize();
  32. if (size > 50 * 1024 * 1024)
  33. {
  34. throw new IOException("文件大小不能超过50mb");
  35. }
  36. List<String> exts= Arrays.asList(
  37. // 图片
  38. "bmp", "gif", "jpg", "jpeg", "png",
  39. // word excel powerpoint
  40. "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
  41. // 压缩文件
  42. "rar", "zip", "gz", "bz2",
  43. // 视频格式
  44. "mp4", "avi", "rmvb",
  45. // pdf
  46. "pdf" );
  47. String fileName = file.getOriginalFilename();
  48. String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
  49. if (!exts.contains(extension)){
  50. throw new IOException("此类文件不能上传");
  51. }
  52. String newFileName = System.currentTimeMillis()+"."+extension;
  53. String filePath=baseDir + "/" + newFileName;
  54. File desc = new File(filePath);
  55. if (!desc.exists())
  56. {
  57. if (!desc.getParentFile().exists())
  58. {
  59. desc.getParentFile().mkdirs();
  60. }
  61. }
  62. String absPath = desc.getAbsolutePath();
  63. file.transferTo(Paths.get(absPath));
  64. return filePath;
  65. }
  66. catch (Exception e)
  67. {
  68. throw new IOException(e.getMessage(), e);
  69. }
  70. }
  71. /**
  72. * 根据文件路径上传
  73. * @param baseDir 相对应用的基目录
  74. * @param file 上传的文件
  75. * @return 文件名称
  76. * @throws IOException
  77. */
  78. public static String uploadImage(String baseDir, MultipartFile file) throws IOException {
  79. try {
  80. if (file.isEmpty()) {
  81. throw new IOException("无文件");
  82. }
  83. int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
  84. if (fileNamelength > 50) {
  85. throw new IOException("文件名不能超过50");
  86. }
  87. long size = file.getSize();
  88. if (size > 20 * 1024 * 1024) {
  89. throw new IOException("文件大小不能超过20mb");
  90. }
  91. List<String> exts = Arrays.asList("bmp", "gif", "jpg", "jpeg", "png");
  92. String fileName = file.getOriginalFilename();
  93. String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
  94. if (!exts.contains(extension)) {
  95. throw new IOException("此类文件不能上传");
  96. }
  97. String newFileName = System.currentTimeMillis() + "." + extension;
  98. String filePath = baseDir + "/" + newFileName;
  99. File desc = new File(filePath);
  100. if (!desc.exists()) {
  101. if (!desc.getParentFile().exists()) {
  102. desc.getParentFile().mkdirs();
  103. }
  104. }
  105. String absPath = desc.getAbsolutePath();
  106. file.transferTo(Paths.get(absPath));
  107. return filePath;
  108. } catch (Exception e) {
  109. throw new IOException(e.getMessage(), e);
  110. }
  111. }
  112. }