4 – SpringBoot implementation file upload and download
January 9, 2024
by 李龍
No Comments
There is the concept of flying in the sky, there must be the realization of landing
Concept + code implementation is the feature of this article, the tutorial will cover complete graphic tutorials, code cases
The end of the article supporting self-test interview questions, learn more solid technology self-test
The concept ten times is not as good as the code once, my friend, I hope you can type all the code cases in the article once
Big brother big sister happy New Year, like forwarding do not less
SpringBoot graphic series tutorials technical outline
Teacher Deer’s Java notes
SpringBoot illustrated tutorial series article directory
SpringBoot图文教程1 “concept + case mind mapping” “basics”
Use “logback” and “log4j” to log.
SpringBoot graphic tutorial 3 – “‘ first love ‘complex” integrated Jsp
preface
From the previous three tutorials, you can already implement a Web project, but the functionality of the project is still a little simple, and the following articles will gradually improve and add new features.
Everything in this article will be done in the previous demo. If you don’t have a written demo, go to the Git repository and download it: gitee.com/bingqilinpe…
File upload
1. Prepare the upload page
<formaction="Path..."method="post"enctype="multipart/form-data"> <inputtype="file"name="aa"> <inputtype="submit"value="Upload"> </form> <! -- 1. The form submission mode must be POST 2. The encType attribute of the form must be multipart/form-data 3. The name of the background variable must be the same as the name attribute of the file selection 4. Action Specifies the path to write Controller methods --> Copy the code
2. Write the controller
@Controller @RequestMapping("/file") publicclassFileController{ @RequestMapping("/upload") public String upload(MultipartFile aa, HttpServletRequest request)throws IOException { // Get the path to the upload folder String realPath = request.getRealPath("/upload"); // Write the uploaded file to the upload folder aa.transferTo(new File(realPath,aa.getOriginalFilename()));// File upload return"index"; } } Copy the code
3. Change the file upload size
The size of the uploaded file exceeds the default value of 10M nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (38443713) exceeds the configured maximum (10485760) Copy the code
In the application.properties configuration file
File download
1. Provide the link to download the file
<ahref=".. /file/download? fileName=corejava.txt">corejava.txt</a> Copy the code
2. Develop controllers
@RequestMapping("/download") publicvoiddownload(String fileName, HttpServletRequest request, HttpServletResponse response)throws Exception { // Get the path to the upload folder String realPath = request.getRealPath("/upload"); // Read files by stream FileInputStream is = new FileInputStream(new File(realPath, fileName)); // Get the response flow ServletOutputStream os = response.getOutputStream(); // Set the response header information response.setHeader("content-disposition"."attachment; fileName="+ URLEncoder.encode(fileName,"UTF-8")); // Write out the file read by the file input stream through the response stream IOUtils.copy(is,os); / / close the flow IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } Copy the code
conclusion
Congratulations on completing this chapter. A round of applause! If this article is helpful to you, please help to like, comment, retweet, this is very important to the author, thank you.
Let’s review the learning objectives of this article again
Master the use of file upload and file download in SpringBoot
To learn more about SpringBoot, stay tuned for this series of tutorials.
Below, I have prepared some self-test interview questions and project cases for my friend Meng. I hope you can become a hot iron and consolidate your knowledge.
Test your answers to the previous interview questions
Meet exam highlights gitee.com/bingqilinpe…
Self-test interview questions (see next section for answers)
SpringMvc file upload steps
Steps to download the SpringMvc file
The last self-test project to achieve small case answers
See code cloud warehouse gitee.com/bingqilinpe…
Self-test implementation project short case (see next issue for answers)
This demand:
Practice article demo
Ask for attention, ask for likes, ask for retweets
Welcome to pay attention to my public account: Teacher Lu’s Java notes, will update Java technology graphic tutorials and video tutorials in the long term, Java learning experience, Java interview experience and Java actual combat development experience.