静态页面编写
编码类型设置为 multipart/form-data 支持文本和文件上传,输入类型为 file
1 2 3 4 5 6 7 8 9 10 11 12
| <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form action="/fileUploadController" method="post" enctype="multipart/form-data"> <input type="file" name="file" value="上传"> <input type="submit"> </form> </body> </html>
|
Controller编写
1 2 3 4 5 6 7 8 9
| @RestController public class FileUploadController { @PostMapping("/fileUploadController") public String fileUpload(MultipartFile file) throws IOException { System.out.println(file.getOriginalFilename()); file.transferTo(new File("f:/"+file.getOriginalFilename())); return "OK"; } }
|
设定上传文件大小和上传总量
在 application.properties 中配置文件上传限制
1 2 3 4
| spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=20MB
|