| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.dk.mdm.controller;
- import com.alibaba.fastjson.JSONObject;
- import com.dk.common.infrastructure.enums.ErrorCodeEnum;
- import com.dk.common.response.ResponseResultUtil;
- import com.dk.common.response.ResponseResultVO;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.IOException;
- import java.time.LocalDateTime;
- import java.util.UUID;
- @RestController
- @RequestMapping("/file")
- public class FileController {
- @Value("${upload-path}")
- private String uploadPath;
- /**
- * @desc : 上传文件
- * @author : 洪旭东
- * @date : 2023-06-26 14:09
- */
- @ApiOperation(value = "上传文件", notes = "上传文件")
- @PostMapping("upload")
- public ResponseResultVO<?> upload(@RequestPart("file") MultipartFile file, @RequestParam("folder") String folder) {
- if (file.isEmpty()) {
- return ResponseResultUtil.error(ErrorCodeEnum.FILE_UPLOAD_FAIL.getCode(), ErrorCodeEnum.FILE_UPLOAD_FAIL.getMessage());
- }
- //生产文件名称
- String fileName = file.getOriginalFilename();
- String uuidName = UUID.randomUUID().toString() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
- String filePath = this.createDirByPath(folder) + uuidName;
- File dest = new File(filePath);
- try {
- file.transferTo(dest);
- //组装参数后返回
- JSONObject json = new JSONObject();
- json.put("path", filePath.replace(uploadPath, ""));
- json.put("name", fileName);
- json.put("type",getFileType(fileName));
- json.put("createTime", LocalDateTime.now());
- return ResponseResultUtil.success(json);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return ResponseResultUtil.error(ErrorCodeEnum.FILE_UPLOAD_FAIL.getCode(), ErrorCodeEnum.FILE_UPLOAD_FAIL.getMessage());
- }
- /**
- * @desc : 获取文件类型
- * @author : 周兴
- * @date : 2024-03-27 14:09
- */
- private String getFileType(String fileName) {
- String[] arr = fileName.split(".");
- String fileType = arr[arr.length - 1].toLowerCase();
- if (fileType == "jpg" || fileType == "jepg" || fileType == "png") {
- return "img";
- }
- if (fileType == "mp4" || fileType == "avi" || fileType == "mov") {
- return "video";
- }
- if (fileType == "xls" || fileType == "xlsx") {
- return "excel";
- }
- if (fileType == "pdf") {
- return "pdf";
- }
- return "";
- }
- /**
- * @desc : 根据类型创建目录文件夹
- * @author : 洪旭东
- * @date : 2023-06-26 14:10
- */
- private String createDirByPath(String folder){
- //斜线
- final String diagonal = "/";
- String path = folder + diagonal;
- StringBuilder base = new StringBuilder(uploadPath);
- File baseDir = new File(uploadPath);
- if (!baseDir.exists()) {
- baseDir.mkdir();
- }
- //按路径新建文件夹
- for (String s : path.split(diagonal)) {
- base.append(diagonal).append(s);
- File dir = new File(base.toString());
- if (!dir.exists()) {
- dir.mkdir();
- }
- }
- return base+diagonal;
- }
- }
|