单机版对象存储,ex1-1

This commit is contained in:
Xudong 2022-04-24 17:44:36 +08:00
parent 97c77ac92e
commit a8f78eaf1e
3 changed files with 70 additions and 7 deletions

View File

@ -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<String,String> greeting() {
Map<String,String>result = 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();
}
}

View File

@ -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;
}
}

View File

@ -2,6 +2,10 @@ spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB