From a8f78eaf1e5048a06b225649f5a0014d5233357a Mon Sep 17 00:00:00 2001 From: Xudong Date: Sun, 24 Apr 2022 17:44:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E6=9C=BA=E7=89=88=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=AD=98=E5=82=A8,ex1-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ObjectsController.java | 43 ++++++++++++++++--- .../result/Result.java | 30 +++++++++++++ src/main/resources/application.yaml | 4 ++ 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/main/java/cn/edu/zjvtit/distributedobjectstorage/result/Result.java diff --git a/src/main/java/cn/edu/zjvtit/distributedobjectstorage/controller/ObjectsController.java b/src/main/java/cn/edu/zjvtit/distributedobjectstorage/controller/ObjectsController.java index b440017..40258de 100644 --- a/src/main/java/cn/edu/zjvtit/distributedobjectstorage/controller/ObjectsController.java +++ b/src/main/java/cn/edu/zjvtit/distributedobjectstorage/controller/ObjectsController.java @@ -1,19 +1,48 @@ package cn.edu.zjvtit.distributedobjectstorage.controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; +import cn.edu.zjvtit.distributedobjectstorage.result.Result; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.util.FileCopyUtils; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; @RestController public class ObjectsController { - @GetMapping("/hello") - public Map greeting() { - Mapresult = new HashMap<>(); - result.put("message","helloword"); - return result; + @PutMapping("/objects") + public String putObject(@RequestParam(value = "file", required = true) MultipartFile file, HttpServletResponse response) throws IOException { + // 上传文件 + return new Result( "success","10000").toString(); + } + + + + + + @GetMapping("/objects/{filename}") + public String getObject(HttpServletResponse response, @PathVariable("filename") String filename){ + File file = new File("./uploads/"+ filename); + if(!file.exists()){ + return new Result("file not exists","10001").toString(); + } + response.reset(); + response.setContentType("application/octet-stream"); + response.setCharacterEncoding("utf-8"); + response.setContentLength((int) file.length()); + response.setHeader("Content-Disposition", "attachment;filename=" + filename ); + + try(BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()))) { + // 下载文件 + } catch (IOException e) { + return new Result("file not exists","10001").toString(); + } + return new Result("file not exists","10000").toString(); } } diff --git a/src/main/java/cn/edu/zjvtit/distributedobjectstorage/result/Result.java b/src/main/java/cn/edu/zjvtit/distributedobjectstorage/result/Result.java new file mode 100644 index 0000000..654344a --- /dev/null +++ b/src/main/java/cn/edu/zjvtit/distributedobjectstorage/result/Result.java @@ -0,0 +1,30 @@ +package cn.edu.zjvtit.distributedobjectstorage.result; + + +public class Result { + private String Message; + private String code; + + public Result(String msg, String code) { + this.Message = msg; + this.code = code; + } + + public String getMessage() { + return Message; + } + + public void setMessage(String message) { + this.Message = message; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } +} + + diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index d38bd5b..a1cdac1 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -2,6 +2,10 @@ spring: autoconfigure: exclude: - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration + servlet: + multipart: + max-file-size: 100MB + max-request-size: 100MB