In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "SpringBoot2 how to achieve file upload logic based on Yml configuration", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "SpringBoot2 how to achieve file upload logic based on Yml configuration" this article.
I. File upload
File upload is a very common function in project development, such as profile photo upload, various document data upload and so on. SpringBoot uses MultiPartFile to receive the file file from the form, and then performs the upload file. This case is based on the yml configuration in SpringBoot2.0, which manages the common attributes of file uploads. This case demonstrates single file upload and multiple file upload.
2. Build file upload interface 1, introduce page template Jar package org.springframework.boot spring-boot-starter-thymeleaf2, write simple upload page 1, single file upload person: file 1: 2, multiple file upload user: file 1: file 2: 3, configure page entry import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping @ Controllerpublic class PageController {/ * upload page * / @ GetMapping ("/ uploadPage") public String uploadPage () {return "upload";} 3. Integration with SpringBoot2.0 1, core configuration file
Individual restrictions on uploading files
Max-file-size: 5MB
Total size limit for uploaded files
Max-request-size: 6MB
Spring: application: # Application name name: node14-boot-file servlet: multipart: # enable enabled: true # upload file single limit max-file-size: 5MB # General limit max-request-size: 6MB2, file upload core code
If the size of a single file exceeds 1MB, an exception is thrown
FileSizeLimitExceededException:
If the total size of the uploaded file exceeds 6MB, an exception is thrown
SizeLimitExceededException:
This fully verifies the configuration in the YML file, which is valid and correct.
Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.util.Map;@RestControllerpublic class FileController {private static final Logger LOGGER = LoggerFactory.getLogger (FileController.class) / * Test single file upload * / @ RequestMapping ("/ upload1") public String upload1 (HttpServletRequest request, @ RequestParam ("file") MultipartFile file) {Map paramMap = request.getParameterMap (); if (! paramMap.isEmpty ()) {LOGGER.info ("paramMap = > > {}", paramMap) } try {if (! file.isEmpty ()) {/ / print file basic information LOGGER.info ("Name = = > > {}", file.getName ()); LOGGER.info ("OriginalFilename = = > > {}", file.getOriginalFilename ()); LOGGER.info ("ContentType = = > > {}", file.getContentType ()) LOGGER.info ("Size = > > {}", file.getSize ()); / / File output address String filePath = "F:/boot-file/"; new File (filePath). Mkdirs (); File writeFile = new File (filePath, file.getOriginalFilename ()); file.transferTo (writeFile) } return "success";} catch (Exception e) {e.printStackTrace (); return "system exception" }} / * Test multiple file upload * / @ RequestMapping ("/ upload2") public String upload2 (HttpServletRequest request, @ RequestParam ("file") MultipartFile [] fileList) {Map paramMap = request.getParameterMap (); if (! paramMap.isEmpty ()) {LOGGER.info ("paramMap = = > > {}", paramMap) } try {if (fileList.length > 0) {for (MultipartFile file:fileList) {/ / print file basic information LOGGER.info ("Name = = > > {}", file.getName ()); LOGGER.info ("OriginalFilename = = > > {}", file.getOriginalFilename ()) LOGGER.info ("ContentType = > > {}", file.getContentType ()); LOGGER.info ("Size = > > {}", file.getSize ()); / / File output address String filePath = "F:/boot-file/"; new File (filePath) .mkdirs () File writeFile = new File (filePath, file.getOriginalFilename ()); file.transferTo (writeFile);} return "success";} catch (Exception e) {return "fail" } the above is all the contents of the article "how SpringBoot2 implements file upload logic based on Yml configuration". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.