Implementation of the "Servlet" Java web file download feature

Source: Internet
Author: User
Tags pack stub java web tomcat server

Requirement: Implement a webpage with file download function, main download compress package and picture

Two methods of implementation:

one: Download via hyperlinksin an HTML page, link to the address of the file you want to download via hyperlinks [HTML]View PlainCopy
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>insert title here</title>
  6. </head>
  7. <body>
  8. <h1> download files via links </h1>
  9. <a href="/day06/download/cors.zip"> Compression pack </a>
  10. <a href="/day06/download/1.png"> Picture </a>
  11. </body>
  12. </html>

Where Day06/download is the document path, the program structure of this instance is as follows:
after the program is run, you can download it by clicking the download document
However, there will be a problem, that is, click to download the compressed package will pop up the download page, but the download picture when the browser opened the image directly, no download.
This is because when you download a file from a hyperlink, the browser opens directly if the browser can recognize the file format. Download is only possible if the file format is not recognized by the browser. Therefore, the second method is used to implement the download function. Second: Through the servlet program to achieve the downloadthe principle of downloading a file through a servlet is to read the target program through the servlet and return the resource to the client. [HTML]View PlainCopy
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>insert title here</title>
  6. </head>
  7. <body>
  8. <h1> download files via links </h1>
  9. <a href="/day06/download/cors.zip"> Compression pack </a>
  10. <a href="/day06/download/1.png"> Picture </a>
  11. <h1> download files through the servlet program </H1>
  12. <a href="/day06/servletdownload?filename=cors.zip"> Compression pack </a>
  13. <a href="/day06/servletdownload?filename=1.png"> Picture </a>
  14. </body>
  15. </html>

Where/day06/servletdownload is the mapping path to the servlet programthen create a new servlet with the name Servletdownload,url mapped to/servletdownload
Add the following code: [Java]View PlainCopy
  1. Package com.lsgjzhuwei.servlet.response;
  2. Import Java.io.FileInputStream;
  3. Import Java.io.FileOutputStream;
  4. Import java.io.IOException;
  5. Import Java.io.InputStream;
  6. Import Java.io.OutputStream;
  7. Import javax.servlet.ServletException;
  8. Import Javax.servlet.annotation.WebServlet;
  9. Import Javax.servlet.http.HttpServlet;
  10. Import Javax.servlet.http.HttpServletRequest;
  11. Import Javax.servlet.http.HttpServletResponse;
  12. /**
  13. * Servlet Implementation Class Servletdownload
  14. */
  15. @WebServlet (asyncsupported = true, Urlpatterns = { "/servletdownload"})
  16. Public class Servletdownload extends HttpServlet {
  17. private Static final long serialversionuid = 1L;
  18. /** 
  19. * @see Httpservlet#httpservlet ()
  20. */
  21. Public Servletdownload () {
  22. super ();
  23. //TODO auto-generated constructor stub
  24. }
  25. /** 
  26. * @see Httpservlet#doget (httpservletrequest request, httpservletresponse response)
  27. */
  28. protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
  29. //TODO auto-generated method stub
  30. //Get request file name
  31. String filename = request.getparameter ("filename");
  32. SYSTEM.OUT.PRINTLN (filename);
  33. //Set file MIME type
  34. Response.setcontenttype (Getservletcontext (). GetMimeType (filename));
  35. //Set content-disposition
  36. Response.setheader ("content-disposition", "attachment;filename=" +filename);
  37. //Read the target file and write the target file to the client via response
  38. //Get the absolute path to the destination file
  39. String fullfilename = Getservletcontext (). Getrealpath ("/download/" + filename);
  40. //system.out.println (fullfilename);
  41. //Read file
  42. InputStream in = new FileInputStream (Fullfilename);
  43. OutputStream out = Response.getoutputstream ();
  44. //write file
  45. int b;
  46. While ((B=in.read ())! =-1)
  47. {
  48. Out.write (b);
  49. }
  50. In.close ();
  51. Out.close ();
  52. }
  53. /** 
  54. * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response)
  55. */
  56. protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
  57. //TODO auto-generated method stub
  58. }
  59. }

To restart the Tomcat server, you can download the compressed package and the image.

Original connection: Java Web file Download function implementation

Implementation of the "Servlet" Java web file download feature

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.