FileController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.dk.mdm.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  4. import com.dk.common.response.ResponseResultUtil;
  5. import com.dk.common.response.ResponseResultVO;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.web.bind.annotation.*;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.time.LocalDateTime;
  13. import java.util.UUID;
  14. @RestController
  15. @RequestMapping("/file")
  16. public class FileController {
  17. @Value("${upload-path}")
  18. private String uploadPath;
  19. /**
  20. * @desc : 上传文件
  21. * @author : 洪旭东
  22. * @date : 2023-06-26 14:09
  23. */
  24. @ApiOperation(value = "上传文件", notes = "上传文件")
  25. @PostMapping("upload")
  26. public ResponseResultVO<?> upload(@RequestPart("file") MultipartFile file, @RequestParam("folder") String folder) {
  27. if (file.isEmpty()) {
  28. return ResponseResultUtil.error(ErrorCodeEnum.FILE_UPLOAD_FAIL.getCode(), ErrorCodeEnum.FILE_UPLOAD_FAIL.getMessage());
  29. }
  30. //生产文件名称
  31. String fileName = file.getOriginalFilename();
  32. String uuidName = UUID.randomUUID().toString() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
  33. String filePath = this.createDirByPath(folder) + uuidName;
  34. File dest = new File(filePath);
  35. try {
  36. file.transferTo(dest);
  37. //组装参数后返回
  38. JSONObject json = new JSONObject();
  39. json.put("path", filePath.replace(uploadPath, ""));
  40. json.put("name", fileName);
  41. json.put("type",getFileType(fileName));
  42. json.put("createTime", LocalDateTime.now());
  43. return ResponseResultUtil.success(json);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. return ResponseResultUtil.error(ErrorCodeEnum.FILE_UPLOAD_FAIL.getCode(), ErrorCodeEnum.FILE_UPLOAD_FAIL.getMessage());
  48. }
  49. /**
  50. * @desc : 获取文件类型
  51. * @author : 周兴
  52. * @date : 2024-03-27 14:09
  53. */
  54. private String getFileType(String fileName) {
  55. String[] arr = fileName.split(".");
  56. String fileType = arr[arr.length - 1].toLowerCase();
  57. if (fileType == "jpg" || fileType == "jepg" || fileType == "png") {
  58. return "img";
  59. }
  60. if (fileType == "mp4" || fileType == "avi" || fileType == "mov") {
  61. return "video";
  62. }
  63. if (fileType == "xls" || fileType == "xlsx") {
  64. return "excel";
  65. }
  66. if (fileType == "pdf") {
  67. return "pdf";
  68. }
  69. return "";
  70. }
  71. /**
  72. * @desc : 根据类型创建目录文件夹
  73. * @author : 洪旭东
  74. * @date : 2023-06-26 14:10
  75. */
  76. private String createDirByPath(String folder){
  77. //斜线
  78. final String diagonal = "/";
  79. String path = folder + diagonal;
  80. StringBuilder base = new StringBuilder(uploadPath);
  81. File baseDir = new File(uploadPath);
  82. if (!baseDir.exists()) {
  83. baseDir.mkdir();
  84. }
  85. //按路径新建文件夹
  86. for (String s : path.split(diagonal)) {
  87. base.append(diagonal).append(s);
  88. File dir = new File(base.toString());
  89. if (!dir.exists()) {
  90. dir.mkdir();
  91. }
  92. }
  93. return base+diagonal;
  94. }
  95. }