Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (31) Springboot receive files for the server

Source: Internet
Author: User

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 limit

If 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.