In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how SpringMVC can upload and download files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows
0. Environmental preparation
1.maven dependence
Org.junit.jupiter junit-jupiter-api 5.7.0 test javax.servlet javax.servlet-api 3.1.0 provided org.springframework spring-webmvc 5.2.6.RELEASE commons-io commons-io 2.8.0 commons-fileupload commons-fileupload 1.3.3
2.springConfig . Xml profile
3.web.xml configuration
Web org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springConfig.xml 1 web * .mvc characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 forRequestEncoding true forResponseEncoding true characterEncodingFilter / * 1. File upload
There are three ways to upload files:
Single file single field
Multiple file single field
Multiple files and multiple fields
Note:
1. Submit the post request for the form
2. There must be enctype= "multipart/form-data" in the from attribute.
3. For multiple files in a single field: the attribute in the input box must be: multiple= "multiple"
4. The attribute name in the form must be consistent with the backend parameters
1. Front-end code
File upload (single file single field upload)
File upload (multi-file single-field upload)
File upload (multi-file and multi-field upload)
two。 Back-end code
Import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpSession;import java.io.File;import java.io.IOException / * @ author compass * @ version 1.0 * @ date 2021-05-11 14:33 * / @ Controllerpublic class UploadIFileController {/ / handle single file upload @ PostMapping ("/ uploadFile1.mvc") public ModelAndView uploadFile1 (MultipartFile file, HttpSession session) throws IOException {ModelAndView view = new ModelAndView (); / / get the file name String filename=file.getOriginalFilename (); System.out.println ("file name:" + filename) If (! file.isEmpty ()) {/ / determine the suffix if of the file (filename.endsWith (".jpg") | | filename.endsWith (".png") | | filename.endsWith (".txt")); / / set the save path of the file String savePath= "C:\ Users\ 14823\ IdeaProjects\ springMVC\ fileupload\ src\ main\ webapp\ WEB-INF\ file" File srcFile = new File (savePath,filename); / / execute file save operation file.transferTo (srcFile); view.setViewName ("forward:/uploadSuccess.jsp");} else {view.setViewName ("forward:/uploadFailed.jsp");} return view } / / handle multiple file uploads @ PostMapping ("/ uploadFile2.mvc") public ModelAndView uploadFile2 (MultipartFile [] file,HttpSession session) throws IOException {ModelAndView view = new ModelAndView (); / / set the file save path String savePath= "C:\ Users\ 14823\ IdeaProjects\ springMVC\ fileupload\ src\ main\ webapp\ WEB-INF\ file"; String [] filenames = new String [file.length] / / as long as the uploaded file is empty or does not meet the specified type, for (int I = 0; I 0) {filename= URLEncoder.encode (filename, "UTF-8"); downloadFile=filename.replaceAll ("+", "% 20");} else {downloadFile=new String (filename.getBytes ("UTF-8"), "ISO-8859-1") } File file = new File (path,filename); HttpHeaders header = new HttpHeaders (); header.setContentDispositionFormData ("attachment", downloadFile); header.setContentType (MediaType.APPLICATION_OCTET_STREAM); ResponseEntity result = new ResponseEntity (FileUtils.readFileToByteArray (file), header, HttpStatus.OK); return result;}} Thank you for reading! This is the end of the article on "how to upload and download SpringMVC files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.