Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SpringBoot receive multi-file files uploaded through http

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how SpringBoot receives multi-file files uploaded through http". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how SpringBoot receives multi-file files uploaded through http".

Receive multi-file files uploaded through http.

Construction project

For example to create a springmvc project you need to start with spring-boot-starter-thymeleaf and spring-boot-starter-web dependencies. For example, to be able to upload files to the server, you need to add tags to the web.xml to do the relevant configuration, but in the sringboot project, it has been done automatically for you, so you do not need to do any configuration.

Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf create file upload controller

Post the code directly:

@ 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 ("files", storageService .loadAll () .map (path-> MvcUriComponentsBuilder .fromMethodName (FileUploadController.class, "serveFile") Path.getFileName () .toString () .build () .toString () .build (Collectors.toList () Return "uploadForm";} @ GetMapping ("/ files/ {filename:.+}") @ ResponseBody public ResponseEntity 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 indicates the c of its previous Spring mvc through the @ Controller annotation. Each method passes through

The @ GetMapping or @ PostMapping annotations indicate their http method.

GET / get the 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 files, let's make an interface:

In src/main/resources/templates/uploadForm.html

File to upload: upload file size limit

If you need to limit the size of the uploaded file, just add the following to the src/main/resources/application.properties of the springboot project:

Spring.http.multipart.max-file-size=128KBspring.http.multipart.max-request-size=128KB thank you for your reading, the above is the content of "how SpringBoot receives multi-file files uploaded through http". After the study of this article, I believe you have a deeper understanding of how SpringBoot receives multi-file files uploaded through http, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report