Building projects
Create a SPRINGMVC project for example you need spring-boot-starter-thymeleaf and spring-boot-starter-web to start relying. As an example to be able to upload files on the server, you need to add tags to the Web. XML to do the related configuration, but in the Sringboot project, it has been done for you automatically, so you do not 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
Direct Sticker Code:
@Controllerpublic class Fileuploadcontroller {private final storageservice storageservice; @Autowired public Fileuploadcontroller (Storageservice storageservice) {this.storageservice = Storageservice; } @GetMapping ("/") public String Listuploadedfiles (model model) throws IOException {Model.addattribute ("fi Les ", Storageservice. Loadall (). Map (Path, Mvcuricomponentsbuild Er. 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 shows itself the last spring MVC C by @controller annotations. Each method passes
@GetMapping or @postmapping annotations indicate your own HTTP method.
- Get/Get List of uploaded files
- Get/files/{filename} download files that already exist on the server
- POST/upload files to the server
Create a simple HTML template
To show the process of uploading a file, we do an interface:
In src/main/resources/templates/uploadform.html
Upload File size limitIf you need to limit the size of the upload file is also very simple, just need to add the following in the Springboot project src/main/resources/application.properties:
spring.http.multipart.max-file-size=128kbspring.http.multipart.max-request-size=128kb
Source Source
Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (31) Springboot receive files for the server