Use Spring Boot and Thymeleaf upload files
Spring Boot MultipartFile usage characteristics to receive and process uploaded files, the present exemplary Thymeleaf used to process the front page.
Quick Start:
1, add dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2, application.properties configuration information
# The maximum supported file spring.servlet.multipart.max -file-size = 100MB # file requests a maximum limit spring.servlet.multipart.max -request-size = 100MB
# In addition to the above configuration too, common configuration information is as follows:
spring.servlet.multipart.enabled = to true, whether to support the multipart upload files
spring.servlet.multipart.file-size-threshold = 0, support files written to disk
spring.servlet. multipart.location =, the temporary directory to upload files
spring.servlet.multipart.max-file-size = 10Mb, supports maximum file size
spring.servlet.multipart.max-request-sizee = 10Mb, maximum support request size
spring.servlet. multipart.resolve-lazily = false, whether lazy load support multipart upload files
3, start class
@SpringBootApplication public class FileUploadWebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(FileUploadWebApplication.class, args); } //Tomcat large file upload connection reset @Bean public TomcatServletWebServerFactory tomcatEmbedded() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> { if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) { //-1 means unlimited ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1); } }); return tomcat; } }
TomcatServletWebServerFactory () method is to solve the problems uploading files larger than 10M connection reset
4, simple front page:
Single file upload page upload.html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <h1>Spring Boot file upload example</h1> <form method="POST" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /><br/><br/> <input type="submit" value="Submit" /> </form> </body> </html>
Multiple file upload page uploadMore.html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <h1>Spring Boot files upload example</h1> <form method="POST" action="/uploadMore" enctype="multipart/form-data"> <input type="file" name="file" /><br/><br/> <input type="file" name="file" /><br/><br/> <input type="file" name="file" /><br/><br/> <input type="submit" value="Submit" /> </form> </body> </html>
Upload results page: uploadStatus.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <body> <h1>Spring Boot - Upload Status</h1> <div th:if="${message}"> <h2 th:text="${message}"/> </div> </body> </html>
5, background upload control class
@Controller public class UploadController { // file storage directory Private String upload_path = "E: TEMP // //" ; // jump upload page @RequestMapping ( "/" ) public String index () { return "Upload" ; } @GetMapping ( "/ more" ) public String uploadMore () { return "uploadMore" ; } // plurality of file upload @RequestMapping ( "/ uploadMore" ) public String moreFileUpload (@RequestParam ( "file") MultipartFile[] files,RedirectAttributes redirectAttrs) { if(files.length ==0) { redirectAttrs.addFlashAttribute("message", "Please select a file to upload"); return "redirect:uploadStatus"; } for(MultipartFile file :files) { try { byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOAD_PATH+file.getOriginalFilename()); Files.write(path, bytes); } catch (IOException e) { e.printStackTrace(); } } redirectAttrs.addFlashAttribute("message", "You successfully uploaded all files"); return "redirect:/uploadStatus"; } //单个文件上传 @PostMapping("/upload") public String singleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttrs) { if(file.isEmpty()) { redirectAttrs.addFlashAttribute("message", "Please select a file to upload"); return "redirect:uploadStatus"; } try { byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOAD_PATH+file.getOriginalFilename()); Files.write(path, bytes); redirectAttrs.addFlashAttribute("message", "You successfully uploaded '" + file.getOriginalFilename() + "'"); } catch (IOException e) { e.printStackTrace(); } return "redirect:/uploadStatus"; } //跳转结果页面 @RequestMapping("/uploadStatus") public String uploadStatus() { return "uploadStatus"; } }
6, run startup class: FileUploadWebApplication
In the browser, go to:
Multiple file upload:
OK, uploaded successfully.