一.yml
1 2 3 4 5
| spring: servlet: multipart: max-file-size: 2MB max-request-size: 2MB
|
二.upload.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>文件上传页面</title> </head> <body> <h1>文件上传页面</h1> <form method="post" action="/upload" enctype="multipart/form-data"> 选择要上传的文件:<input type="file" name="file"><br> <hr> <input type="submit" value="提交"> </form> </body> </html>
|
三.UploadController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @Controller @Slf4j public class UploadController {
private String path="D:\\IDEA\\filetest\\";
@GetMapping("/") public String uploadPage() { return "upload.html"; }
@PostMapping("/upload") @ResponseBody public String create(@RequestPart MultipartFile file) throws IOException { String fileName = file.getOriginalFilename();
String filePath = path + fileName;
File dest = new File(filePath); Files.copy(file.getInputStream(), dest.toPath()); return "Upload file success : " + dest.getAbsolutePath(); }
}
|
四.@RequestPart
@RequestPart类似于@RequestParam
参考:https://www.baeldung.com/sprint-boot-multipart-requests
https://segmentfault.com/a/1190000038864532
https://segmentfault.com/a/1190000038985065?utm_source=sf-similar-article