<% @ 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 <%>.