The example in this article describes how JSP downloads server files. Share to everyone for your reference, specific as follows:
<% @page import= "Java.io.FileInputStream"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <% @page language=" java "contenttype=" text/html; Charset=utf-8 "%> <% @page import=" Com.ecc.emp.core.Context "%> <% @page import=" Java.io.File "%> <%@
Include file= "http://www.xxxx.com/head.jsp"%> <% boolean loadflag = false;
Servletoutputstream OU = NULL;
FileInputStream FIS = null;
try{Context context= (context) Request.getattribute ("context");
String Accountfilepath = (string) context.getdatavalue ("Accountfilepath"); String Accountfilepath = "E:/comeback/acc_bos_cm/webcontent/upload/product/1336873410414custormerstatement.txt"
;
String accountfilename = (string) context.getdatavalue ("Accountfilename");
String displayfilename = (string) context.getdatavalue ("Displayfilename");
String displayfilename = "CustormerStatement.txt"; String FullPath =accounTfilepath;
Response.reset ();
Response.setcontenttype ("Charset=utf-8");
Response.setcontenttype ("text/html");
Response.setcontenttype ("application/vnd.ms-excel");
string filename = new string (Displayfilename.getbytes ("gb2312"), "iso8859-1");
Response.setheader ("Content-disposition", "attachment; filename=" + filename);
Resolve issues that HTTPS cannot download Response.setheader ("Cache-control", "public");
OU = Response.getoutputstream ();
File File = new file (FullPath);
FIS = new FileInputStream (file);
try {if (FIS!= null) {int filelen = fis.available ();
byte a[] = new Byte[filelen];
Fis.read (a);
Ou.write (a);
Ou.flush ();
} loadflag=true;
catch (Exception e) {System.out.println ("Excel is not exist!");
out = Pagecontext.pushbody ();
}catch (Exception e) {e.printstacktrace ();
}finally{try {fis.close ();
Ou.close ();
OU = NULL; RespOnse.flushbuffer ();
catch (Exception e) {//TODO automatically generates catch blocks E.printstacktrace ();
}}%>
I hope this article will help you with JSP programming.