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
- <! DOCTYPE HTML>
- <html>
- <head>
- <Meta charset="UTF-8">
- <title>insert title here</title>
- </head>
- <body>
- <h1> download files via links </h1>
- <a href="/day06/download/cors.zip"> Compression pack </a>
- <a href="/day06/download/1.png"> Picture </a>
- </body>
- </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
- <! DOCTYPE HTML>
- <html>
- <head>
- <Meta charset="UTF-8">
- <title>insert title here</title>
- </head>
- <body>
- <h1> download files via links </h1>
- <a href="/day06/download/cors.zip"> Compression pack </a>
- <a href="/day06/download/1.png"> Picture </a>
- <h1> download files through the servlet program </H1>
- <a href="/day06/servletdownload?filename=cors.zip"> Compression pack </a>
- <a href="/day06/servletdownload?filename=1.png"> Picture </a>
- </body>
- </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
- Package com.lsgjzhuwei.servlet.response;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Java.io.OutputStream;
- Import javax.servlet.ServletException;
- Import Javax.servlet.annotation.WebServlet;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- /**
- * Servlet Implementation Class Servletdownload
- */
- @WebServlet (asyncsupported = true, Urlpatterns = { "/servletdownload"})
- Public class Servletdownload extends HttpServlet {
- private Static final long serialversionuid = 1L;
- /**
- * @see Httpservlet#httpservlet ()
- */
- Public Servletdownload () {
- super ();
- //TODO auto-generated constructor stub
- }
- /**
- * @see Httpservlet#doget (httpservletrequest request, httpservletresponse response)
- */
- protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
- //TODO auto-generated method stub
- //Get request file name
- String filename = request.getparameter ("filename");
- SYSTEM.OUT.PRINTLN (filename);
- //Set file MIME type
- Response.setcontenttype (Getservletcontext (). GetMimeType (filename));
- //Set content-disposition
- Response.setheader ("content-disposition", "attachment;filename=" +filename);
- //Read the target file and write the target file to the client via response
- //Get the absolute path to the destination file
- String fullfilename = Getservletcontext (). Getrealpath ("/download/" + filename);
- //system.out.println (fullfilename);
- //Read file
- InputStream in = new FileInputStream (Fullfilename);
- OutputStream out = Response.getoutputstream ();
- //write file
- int b;
- While ((B=in.read ())! =-1)
- {
- Out.write (b);
- }
- In.close ();
- Out.close ();
- }
- /**
- * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response)
- */
- protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
- //TODO auto-generated method stub
- }
- }
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