JSP Io stream download file

Source: Internet
Author: User
 
<% @ Page Language = "Java" contenttype = "application/X-msdownload" Import = 'java. io. *, Java. util. * 'pageencoding = "gb2312" %> <% string filenamedisplay = ""; string type = request. getparameter ("type ")! = NULL? Request. getparameter ("type "). trim (): ""; if (type. equals ("1") {filenamedisplay = "paysearch_tracking_code_template.xls";} else if (type. equals ("2") {filenamedisplay = "ao_tracking_code_template.xls";} else if (type. equals ("3") {filenamedisplay = "edm_tracking_code_template.xls";} else if (type. equals ("4") {filenamedisplay = "social_media_tracking_code_template.xls";} else if (type. equals ("5") {filenamedis Play = "online_ad_tracking_code_template.xls";} else if (type. equals ("6") {filenamedisplay = "media_coop_tracking_code_template.xls" ;}else {return;} Java. io. file file = new Java. io. file (application. getrealpath ("/" + filenamedisplay); // system. out. println ("->" + application. getrealpath ("/" + filenamedisplay); // string context = session. getservletcontext (). getrealpath ("/"); string context2 = Application. Getrealpath ("/" + filenamedisplay); // system. Out. println ("==>" + context); If (! File. exists () {out. println ("<SCRIPT> alert ('the file is not exist! In this path => "+ application. getrealpath ("/" + filenamedisplay) + "'); window. close (); </SCRIPT> "); return;} // response. setcontenttype ("application/force-download"); response. setheader ("content-disposition", "attachment; filename =" + filenamedisplay); try {bufferedinputstream is = new bufferedinputstream (New fileinputstream (context2); // inputstream is = application. getresourceasstream ("/" + filenamedispla Y); byte [] bytearray = new byte [10240]; int bytenumber = 0; int filelength = 0; // system. out. println ("dddd1"); servletoutputstream OS = response. getoutputstream (); While (bytenumber = is. read (bytearray ))! =-1) {filelength + = bytenumber; OS. write (bytearray, 0, bytenumber);} OS. flush (); is. close (); OS. close (); out. clear (); out = pagecontext. pushbody ();} catch (exception e) {e. printstacktrace (system. out) ;}%>

Note: No space is allowed between <%>.

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.