This article mainly introduces how to use springboot project as a server to receive multi-file files uploaded through http.
Construction project
For example, to create a spring MVC project, you need spring-boot-starter-thymeleaf and spring-boot-starter-web start dependencies. For example, to upload files to the server, you need to add tags to the web.xml to configure it, but in the sringboot project, it has been done automatically for you, so you don't need to do any configuration.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
Create file upload controller
Paste code directly:
@Controller public class FileUploadController { private final StorageService storageService; @Autowired public FileUploadController(StorageService storageService) { this.storageService = storageService; } @GetMapping("/") public String listUploadedFiles(Model model) throws IOException { model.addAttribute("files", storageService .loadAll() .map(path -> MvcUriComponentsBuilder .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()) .build().toString()) .collect(Collectors.toList())); return "uploadForm"; } @GetMapping("/files/{filename:.+}") @ResponseBody public ResponseEntity<Resource> serveFile(@PathVariable String filename) { Resource file = storageService.loadAsResource(filename); return ResponseEntity .ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"") .body(file); } @PostMapping("/") public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { storageService.store(file); redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!"); return "redirect:/"; } @ExceptionHandler(StorageFileNotFoundException.class) public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { return ResponseEntity.notFound().build(); } }
This class annotates its last Spring mvc c C with the @Controller annotation. Each method passes
@ The GetMapping or @PostMapping annotations indicate their own http methods.
- GET / Get the list of files that have been uploaded
- GET/files/{filename} Download files that already exist on the server
- POST / Upload Files to Server
Create a simple html template
In order to show the process of uploading files, we make an interface:
At src/main/resources/templates/uploadForm.html
<html xmlns:th="http://www.thymeleaf.org"> <body> <div th:if="${message}"> <h2 th:text="${message}"/> </div> <div> <form method="POST" enctype="multipart/form-data" action="/"> <table> <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr> <tr><td></td><td><input type="submit" value="Upload" /></td></tr> </table> </form> </div> <div> <ul> <li th:each="file : ${files}"> <a th:href="${file}" th:text="${file}" /> </li> </ul> </div> </body> </html>
Upload File Size Limitation
If you need to limit the size of uploaded files, it's easy to add the following to the springboot project's src/main/resources/application.properties:
spring.http.multipart.max-file-size=128KB spring.http.multipart.max-request-size=128KB
The architecture code is as follows:
The source code of B2B2C e-commerce platform constructed by Spring Cloud distributed micro-service cloud for large enterprises, Please add Penguin: 10387474626