Servlet File Download

Source: Internet
Author: User

[Introduction]

A ServletFile Download.

 

[Folder structure]

 

[Java code]

1 package COM. zjm. www. servlet; 2 3 Import Java. io. bufferedinputstream; 4 Import Java. io. bufferedoutputstream; 5 import Java. io. file; 6 Import Java. io. fileinputstream; 7 Import Java. io. ioexception; 8 Import Java. io. inputstream; 9 Import Java. io. outputstream; 10 11 import javax. servlet. servletexception; 12 Import javax. servlet. HTTP. httpservlet; 13 Import javax. servlet. HTTP. httpservletrequest; 14 Import J Avax. servlet. HTTP. httpservletresponse; 15 16/** 17 * @ description Servlet File Download 18 * @ author M19 * @ blog http://www.cnblogs.com/xiaoMzjm/20 * @ time 2014/07/3021 */22 public class downloadservlet extends httpservlet {23 24 25 private Static final long serialversionuid = 1l; 26 27 public void doget (httpservletrequest request, httpservletresponse response) 28 throws servletexception, ioexception {29 dopost (request, respon Se); 30} 31 32 public void dopost (httpservletrequest request, httpservletresponse response) 33 throws servletexception, ioexception {34 35 // transcoding, filter is recommended. 36 request. setcharacterencoding ("utf8"); 37 response. setcharacterencoding ("utf8"); 38 39 // get the file name 40 string filename = request. getparameter ("FILENAME"); 41 42 // folder of the file, relative path of the Server File. For Path Problems, refer to another blog: http://www.cnblogs.com/xiaoMzjm/p/3878758.html43 string Path = request. getservletcontext (). getrealpath ("/") + "\ downloadfile \"; 44 // download Path = folder where the file is stored + file name 46 string downloadpath = path + filename; 47 48 // create an input stream connection file and read the file to the stream. 49 file = new file (downloadpath); 50 inputstream FD = new bufferedinputstream (New fileinputstream (File )); 51 52 // determine how many bytes a file can read 53 byte [] buffer = new Te [FCM. available ()]; 54. read (buffer); 55 FS. close (); 56 57 // clear response58 response. reset (); 59 60/** 61 * set the response header, which must be set. 62 * [Introduction to several header files]: 63*1. Content-disposition is an extension of the mime protocol. This protocol allows the browser to pop up a download box. You can also change the file name during download. 64*2. Content-Length describes the transmission length of an HTTP message object. 65 * in HTTP, the length of a message entity differs from that of a message entity. For example, in gzip compression, the length of a message entity is the length before compression, the transmission length of the message entity is the length after gzip compression. 66 */67 response. addheader ("content-disposition", "attachment; filename =" + new string (filename. getbytes ("UTF-8"), "ISO-8859-1"); 68 response. addheader ("Content-Length", "" + file. length (); 69 outputstream out = new bufferedoutputstream (response. getoutputstream (); 70 71/** 72 * configure the MIME type to notify the browser of the format of the downloaded file. 73 * more MIME type and suffix ing can be found in Baidu Library search: "MIME type Daquan" or in the Stamp connection: 74 * http://wenku.baidu.com/link? Url = SIJffXFE68HnvhI39h0c0PtjSzz7v8SoYzo274HcEx8jYRm-V07_WkotLQdVnT_JGyNhIUY_bjQRgnfz_b53rg5EnHD_ist84Pq-uWMYjyG75 */76 response. setcontenttype ("application/octet-stream"); 77 78 // output 79 out. write (buffer); 80 out. flush (); 81 out. close (); 82} 83}

 

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.