File download with Springmvc for file upload and download

Source: Internet
Author: User
Tags save file

Next, the file download needs to obtain the download file storage path, here is only manually fill in, if it is in a specific project, you can save the file name and upload the storage path in the database. Then add a file list of the page to show the file name and file path, and then click on the download when the corresponding file name and path to the background operation.

[Java]View PlainCopy 
  1. /**
  2. * File download requires file name and file address
  3. *
  4. * @author: Tuzongxun
  5. * @Title: Download
  6. * @[email Protected] Name
  7. * @[email protected] Path
  8. * @[email protected]
  9. * @[email protected] IOException
  10. * @returnResponseEntity <byte[]>
  11. * @date APR 28,2016 1:21:32 PM
  12. * @throws
  13. */
  14. @RequestMapping (value = "/downloadfile.do")
  15. Public responseentity<byte[]> Download (@RequestParam ("name") String name,
  16. @RequestParam ("FilePath") String path) throws IOException {
  17. File File = new file (path);
  18. Httpheaders headers = new Httpheaders ();
  19. String fileName = new String (Name.getbytes ("UTF-8"), "iso-8859-1"); In order to solve the problem of Chinese name garbled
  20. Headers.setcontentdispositionformdata ("attachment", fileName);
  21. Headers.setcontenttype (Mediatype.application_octet_stream);
  22. return new responseentity<byte[]> (fileutils.readfiletobytearray (file),
  23. headers, httpstatus.created);
  24. }

HTML page, here is simply fill in the filename and file path, with form form submitted to the background, and then the background will control response in the page pop-up Save file path selection box, of course, here in the background I did not do what processing, if the file does not exist will be error, can be processed accordingly:

[HTML]View PlainCopy  
  1. File Download:
  2. </br> </br>
  3. <form action= "./downloadfile.do"style= "border:1px solid Grey;width:auto;" method="POST">
  4.             file name: < input type= "text"  name= "name" /></br></br >  
  5. File path:<input type="text" name="FilePath"/></br></ br>
  6. <input type="Submit" value= "confirm Download"/>
  7. </form>


The page view is as follows:

If the file does not exist, the error is as follows:

File download with Springmvc for file upload and download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.