Common file Download in Java (ContentType, header, response, out four steps)

Source: Internet
Author: User

common file Download in Java (ContentType, header, response, out four steps)

Sina Weibo: It Imperial College (remember to follow OH) Http://weibo.com/itguozijian

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 ServletContext objects by implementing the Servletcontextaware interface
  11. Private ServletContext ServletContext;
  12. @RequestMapping ("File/download")
  13. public void FileDownload (HttpServletResponse response) {
  14. Gets the site deployment path (via the ServletContext object), which is used to determine the download file location for download
  15. String Path = Servletcontext.getrealpath ("/");
  16. 1. Set the file contenttype type, this setting, will automatically determine the download file type
  17. Response.setcontenttype ("Multipart/form-data");
  18. 2. Set 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. Get the file object 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. Get Servletoutputstream object (out) via response
  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. Write 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. }

Common file Download in Java (ContentType, header, response, out four steps)

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.