Java Web file download feature implementation

Source: Internet
Author: User

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
<! DOCTYPE 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.
<! DOCTYPE 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:
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 SERVLETDOWNL           Oad 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 R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO Auto-generatEd method stub//Gets the request file name string filename = Request.getparameter ("filename"); SYSTEM.OUT.PRINTLN (filename);//Set file MIME type Response.setcontenttype (Getservletcontext (). GetMimeType (filename));// Set Content-dispositionresponse.setheader ("Content-disposition", "attachment;filename=" +filename);//Read the target file, Writes the target file to the client by response//Gets the absolute path of 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.

Java Web file download feature implementation

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.