Java Web Response provides file download functionality

Source: Internet
Author: User
Tags java web

The structure of the WebApp project is as follows:

The contents of the download.html file are as follows:

<! DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert Title here</title></head><body>        <H1>Resources Download:</H1>       <p>Simple use of a tag, only the browser can not parse the file will be downloaded, otherwise it will be directly resolved by the browser.</P>        <a href="/web/resource/a.mp3" >A.mp3</a><br>        <a href="/web/resource/a.exe" >A.exe</a><br>        <a href="/web/resource/a.txt" >A.txt</a><br>        <a href="/web/resource/a.xlsx" >A.xlsx</a><br>        <a href="/web/resource/a.png" >A.png</a><br>        <p>Therefore, using the a tag combined with the response of the servlet instructs the browser not to parse these pending files</P>        <a href="/web/download?filename=a.mp3" >A.mp3</a><br>        <a href="/web/download?filename=a.exe" >A.exe</a><br>        <a href="/web/download?filename=a.txt" >A.txt</a><br>        <a href="/web/download?filename=a.xlsx" >A.xlsx</a><br>        <a href="/web/download?filename=a.png" >A.png</a><br></body></html>

The contents of the Servlet--download.java file to be processed for download are as follows:

 PackageCom.download.servlet;ImportJava.io.FileInputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletOutputStream;ImportJavax.servlet.http.HttpServlet;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/** * Servlet Implementation class Download */ Public  class Download extends httpservlet {Private Static Final LongSerialversionuid =1L; protected void doget(httpservletrequest request, httpservletresponse response)  throws servletexception, IOException{//1. Get the file name of the request downloadString filename = Request.getparameter ("FileName");//2. Getting the file system path for a fileString FilePath = Request.getservletcontext (). Getrealpath ("resource/"+filename);//3. Set the response header to prompt the browser not to parse the response's file data, but instead to parse it in the form of an attachment (attachment), which is the download functionResponse.setcontenttype ( This. Getservletcontext (). GetMimeType (filename); Response.setheader ("Content-disposition","Attachment;filename="+filename);//4. Read the input stream of the file, as well as the output stream of the response, and output the data to the clientInputStream in =NewFileInputStream (FilePath); Servletoutputstream out = Response.getoutputstream ();intLen =0;byte[] buf =New byte[1024x768]; while((Len=in.read (BUF))!=-1) {Out.write (buf,0, Len);    } in.close (); } protected void doPost(httpservletrequest request, httpservletresponse response)  throws servletexception, IOException{doget (request, response); }}

Enter it in the browser address bar http://localhost:8080/DownloadServlet/download.html .

Note: If you think this article is not bad please click on the lower right corner of the recommendation, your support can inspire the author more enthusiasm for writing, thank you very much!

Java Web Response provides file download functionality

Related Article

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.