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 upload and download files through Spring MVC

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

Share

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

Today, the editor will share with you the relevant knowledge points about how to upload and download files through Spring MVC. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

File upload

1. Import mainly depends on

Commons-fileupload commons-fileupload 1.3.3 javax.servlet javax.servlet-api 4.0.1

2. Configure bean:multipartResolver (Note: the id of this bena must be: multipartResolver, otherwise uploading the file will report a 400th error! )

3 、 index.jsp

4 、 Controller

@ RestControllerpublic class FileController {/ / @ RequestParam ("file") encapsulates the files obtained by the name=file control into CommonsMultipartFile objects / / batch upload CommonsMultipartFile can be an array @ RequestMapping ("/ upload") public String fileUpload (@ RequestParam ("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {/ / get the file name: file.getOriginalFilename (); String uploadFileName = file.getOriginalFilename (); / / if the file name is empty, go straight back to the home page! If (".equals (uploadFileName)) {return" redirect:/index.jsp ";} System.out.println (" upload file name: "+ uploadFileName); / / upload path save settings String path = request.getServletContext (). GetRealPath (" / upload "); / / if the path does not exist, create a File realPath = new File (path) If (! realPath.exists ()) {realPath.mkdir ();} System.out.println ("upload File Save address:" + realPath); InputStream is = file.getInputStream (); / / File input stream OutputStream os = new FileOutputStream (new File (realPath, uploadFileName)); / / File output stream / / read and write int len = 0 Byte [] buffer = new byte [1024]; while ((len = is.read (buffer))! =-1) {os.write (buffer, 0, len); os.flush ();} os.close (); is.close (); return "redirect:/index.jsp" } / * * use file.Transto to save uploaded files * / @ RequestMapping ("/ upload2") public String fileUpload2 (@ RequestParam ("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {/ / upload path save settings String path = request.getServletContext () .getRealPath ("/ upload"); File realPath = new File (path) If (! realPath.exists ()) {realPath.mkdir ();} System.out.println ("upload file save address:" + realPath); / / write the file directly by CommonsMultipartFile (note this time) file.transferTo (new File (realPath + "/" + file.getOriginalFilename ()); return "redirect:/index.jsp";}}

File download

Index.jsp

Controller

@ RequestMapping (value = "/ download") public String downloads (HttpServletResponse response, HttpServletRequest request) throws Exception {/ / the address of the image to be downloaded is String path = request.getServletContext () .getRealPath ("/ upload"); String fileName = "basic syntax .jpg"; / / 1. Set the response response header response.reset (); / / set the page not to cache, clear buffer response.setCharacterEncoding ("UTF-8") / / character encoding response.setContentType ("multipart/form-data"); / / binary transfer data / / set response header response.setHeader ("Content-Disposition", "attachment;fileName=" + URLEncoder.encode (fileName, "UTF-8")); File file = new File (path, fileName); / / 2, read file-input stream InputStream input = new FileInputStream (file) / / 3. Write out file-output stream OutputStream out = response.getOutputStream (); byte [] buff = new byte [1024]; int index = 0; / / 4. Perform write operation while ((index = input.read (buff))! =-1) {out.write (buff, 0, index); out.flush ();} out.close (); input.close (); return null } these are all the contents of the article "how to upload and download files through Spring MVC". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.

Share To

Development

Wechat

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

12
Report