|
@@ -0,0 +1,65 @@
|
|
|
|
|
+package com.ydtech.modules.task.controller;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import com.ydtech.core.page.HttpResult;
|
|
|
|
|
+import com.ydtech.modules.task.entity.po.TaskFile;
|
|
|
|
|
+import com.ydtech.modules.task.service.TaskFileService;
|
|
|
|
|
+import io.swagger.annotations.Api;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
|
+
|
|
|
|
|
+@Api(tags = "任务文件controller")
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/task/file")
|
|
|
|
|
+public class TaskFileController {
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${upload.file.path}")
|
|
|
|
|
+ private String uploadPath;
|
|
|
|
|
+
|
|
|
|
|
+ private String taskPath = "/task/";
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private TaskFileService taskFileService;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/upload")
|
|
|
|
|
+ public HttpResult<Object> upload(@RequestParam("file") MultipartFile file,@RequestParam("taskId") String taskId){
|
|
|
|
|
+ if(file.isEmpty()){
|
|
|
|
|
+ return HttpResult.error("文件为空,请重新上传");
|
|
|
|
|
+ }
|
|
|
|
|
+ taskId = taskId.replaceAll(",","");
|
|
|
|
|
+
|
|
|
|
|
+ try{
|
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
|
+ String[] split = file.getOriginalFilename().split("\\.");
|
|
|
|
|
+ String fileType = split[split.length-1];
|
|
|
|
|
+ // 获取文件的字节
|
|
|
|
|
+ byte[] bytes = file.getBytes();
|
|
|
|
|
+ // 指定文件保存路径,这里假设保存在项目的uploads目录下
|
|
|
|
|
+ String destinationFilePath = uploadPath + taskPath + fileName;
|
|
|
|
|
+
|
|
|
|
|
+ // 保存文件到服务器的逻辑,例如使用java.nio.file包的Files类
|
|
|
|
|
+ File dir = new File(uploadPath + taskPath);
|
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
|
+ dir.mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+ file.transferTo(new File(destinationFilePath));
|
|
|
|
|
+ TaskFile taskFile = new TaskFile(taskId,fileName,fileType,destinationFilePath);
|
|
|
|
|
+ taskFileService.save(taskFile);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return HttpResult.error("文件上传失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ return HttpResult.ok("");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|