Springmvc File Upload and download

Source: Internet
Author: User

Upload

1.jar Package with Springmvc.xml

Package the request into multiparthttpservletrequest.

2. Page

Note that method is post

3.action

Method one parsing (request)

Method two (parameter)

Download

  1. @RequestMapping ("/download/{filename}")
  2. Public modelandview Download (@PathVariable ("FileName")
  3. String FileName, httpservletrequest request, httpservletresponse response)
  4. throws Exception {
  5. Response.setcontenttype ("Text/html;charset=utf-8");
  6. Request.setcharacterencoding ("UTF-8");
  7. Java.io.BufferedInputStream bis = null;
  8. Java.io.BufferedOutputStream BOS = null;
  9. String Ctxpath = Request.getsession (). Getservletcontext (). Getrealpath (
  10. "/")  
  11. + " \ \" + "images\\";
  12. String Downloadpath = Ctxpath + fileName;
  13. System.out.println (Downloadpath);
  14. try {
  15. Long filelength = new File (Downloadpath). Length ();
  16. Response.setcontenttype ("application/x-msdownload;");
  17. Response.setheader ("content-disposition", "attachment; Filename= "
  18. + New String (Filename.getbytes ("Utf-8"), "iso8859-1"));
  19. Response.setheader ("Content-length", String.valueof (Filelength));
  20. bis = new Bufferedinputstream (new FileInputStream (Downloadpath));
  21. BOS = New Bufferedoutputstream (Response.getoutputstream ());
  22. byte[] Buff = new byte[2048];
  23. int bytesread;
  24. While (-1! = (Bytesread = bis.read (buff, 0, Buff.length))) {
  25. Bos.write (Buff, 0, Bytesread);
  26. }
  27. } catch (Exception e) {
  28. E.printstacktrace ();
  29. } finally {
  30. if (bis! = null)
  31. Bis.close ();
  32. if (bos! = null)
  33. Bos.close ();
  34. }
  35. return null;
  36. }
  37. }

Springmvc 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.