Java File download

Source: Internet
Author: User

We will go directly to the topic, file download only need four steps:

1. Set the file ContentType type

2. Setting the file header

3. Get Servletoutputstream object (out) via response

4. Write to the output stream (out)

Download code:

I'm using SPRINGMVC here, but the only purpose here is to get the ServletContext object, the object's purpose, as explained in the following example

Download, need to use two jar package: Commons-fileupload.jar and Commons-io.jar

Java code
  1. Import Org.springframework.stereotype.Controller;
  2. Import org.springframework.web.bind.annotation.RequestMapping;
  3. Import Org.springframework.web.context.ServletContextAware;
  4. Import Javax.servlet.ServletContext;
  5. Import Javax.servlet.ServletOutputStream;
  6. Import Javax.servlet.http.HttpServletResponse;
  7. Import java.io.*;
  8. @Controller
  9. Public class Filecontroller implements servletcontextaware{
  10. //spring here is to inject the ServletContext object by implementing the Servletcontextaware interface
  11. private ServletContext ServletContext;
  12. @RequestMapping ("File/download")
  13. public void FileDownload (httpservletresponse response) {
  14. //Get Site Deployment path (via ServletContext object) to determine the location of the download file for download
  15. String path = Servletcontext.getrealpath ("/");
  16. //1. Set the file contenttype type so that it will automatically determine the type of download file
  17. Response.setcontenttype ("Multipart/form-data");
  18. //2. Setting the file header: The last parameter is to set the download file name (if we call A.pdf)
  19. Response.setheader ("content-disposition", "attachment;filename=" +"a.pdf");
  20. Servletoutputstream out;
  21. //The file object is obtained through the path of the files (false there is a download.pdf file in this path)
  22. File File = new file (path + "download/" + "download.pdf");
  23. try {
  24. FileInputStream InputStream = new FileInputStream (file);
  25. //3. Getting Servletoutputstream objects by response (out)
  26. out = Response.getoutputstream ();
  27. int b = 0;
  28. byte[] buffer = new byte[512];
  29. While (b! =-1) {
  30. b = inputstream.read (buffer);
  31. //4. Writing to the output stream (out)
  32. Out.write (buffer,0,b);
  33. }
  34. Inputstream.close ();
  35. Out.close ();
  36. Out.flush ();
  37. } catch (IOException e) {
  38. E.printstacktrace ();
  39. }
  40. }
  41. @Override
  42. public void Setservletcontext (ServletContext servletcontext) {
  43. this.servletcontext = ServletContext;
  44. }
  45. }

Java File 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.