springboot文件上传
-
创建springboot项目
-
编写页面(resources/static/fileupload.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/fileUploadController" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="ok"> </form> </body> </html>
-
编写controller(controller/FileUploadController)
@RestController public class FileUploadController { //文件上传 @PostMapping("/fileUploadController") public String fileUpload(MultipartFile file) throws IOException { System.out.println(file.getOriginalFilename()); file.transferTo(new File("e:/"+file.getOriginalFilename())); return "ok"; } }
-
修改上传文件的大小(resources/application.properties)
#配置单个上传文件大小 spring.servlet.multipart.max-file-size=2MB #配置一次请求中批量上传文件总容量大小 spring.servlet.multipart.max-request-size=10MB