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 to realize File upload and download by SpringMVC

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to upload and download files in SpringMVC". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to upload and download files by SpringMVC"!

I. General configuration

Pom.xml

Org.springframework spring-webmvc 5.3.13 ch.qos.logback logback-classic 1.2.3 javax.servlet javax.servlet-api 4.0.1 provided Org.thymeleaf thymeleaf-spring5 3.0.12.RELEASE commons-fileupload commons-fileupload 1.3.1 org.springframework spring-context 5.3.11 junit Junit 4.12 test org.springframework spring-test 5.3.13 mysql mysql-connector-java 8.0.24 com. Alibaba druid 1.2.8 org.springframework spring-context 5.3.11 javax.servlet javax.servlet-api 4.0.1 provided javax. Servlet.jsp javax.servlet.jsp-api 2.3.3 com.fasterxml.jackson.core jackson-databind 2.12.1 commons-fileupload commons-fileupload 1.3.1

Key jar of upload function

Web.xml

Character set filter characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter character set encoding encoding UTF-8 forceEncoding true characterEncodingFilter / * HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter / * DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springMVC.xml 1 DispatcherServlet /

SpringMVC.xml

Text/html application/json

The key to the realization of page jump, vue file parsing and upload content parsing

File.html content demo

Download 1.jpg avatar:

Second, realize the file download and upload function package com.vector.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.util.MultiValueMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import java.io.File Import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.UUID;@Controllerpublic class FileUpAndDownController {@ RequestMapping ("/ testDown") public ResponseEntity testResponseEntity (HttpSession session) throws IOException {/ / get the ServletContext object ServletContext servletContext = session.getServletContext (); / / get the real path to the file in the server String realPath = servletContext.getRealPath ("/ static/img/1.jpg") / / create input stream InputStream is = new FileInputStream (realPath); / / create byte array byte [] bytes = new byte [is.available ()]; / / read stream to byte array is.read (bytes); / / create HttpHeaders object setting response header information MultiValueMap headers = new HttpHeaders () / / set the download method and the name of the download file / / Content-Disposition fixed reply content format attachment download the filename=1.jpg file name as an attachment / / you can splice the filename into a dynamic named headers.add ("Content-Disposition", "attachment;filename=1.jpg"); / / set the response status code HttpStatus statusCode = HttpStatus.OK / / create ResponseEntity object ResponseEntity responseEntity = new ResponseEntity (bytes, headers, statusCode); / / close the input stream is.close (); return responseEntity;} @ RequestMapping ("/ testUp") public String testUp (@ Value ("multipartResolver") MultipartFile photo,HttpSession session) throws IOException {/ / get the file name String fileName = photo.getOriginalFilename () of the uploaded file / / handle file renaming problem / / duplicate name problem is that writing the same file in java.io overwrites the contents of the original file by default, resulting in the picture being overwritten. / / get the file name suffix String suffixName = fileName.substring (fileName.lastIndexOf (".")); / / use UUID as the file name uuid is a 32-bit random number, it is almost impossible to repeat fileName = UUID.randomUUID (). ToString () + suffixName; / / get the path to the photo directory in the server ServletContext servletContext = session.getServletContext (); String photoPath = servletContext.getRealPath ("photo") File file = new File (photoPath); / / determine whether the path if (! file.exists ()) {file.mkdir ();} String finalPath = photoPath + File.separator + fileName; / / implement upload function photo.transferTo (new File (finalPath)); return "success";}}

Download function test

Upload function test

At this point, I believe you have a deeper understanding of "how to upload and download files in SpringMVC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report