Sometimes a template download, this simple download server already exists file function, you can easily through the JSP file download way to easily realize.
JSP page JS
Copy Code code as follows:
/**
* Export Role
*/
function Exportrole () {
var user_id = $ (' input[name=userlistradio]:checked '). attr (' id ');
if (!user_id | | user_id = = ") {
Showinfo (' Please select User! ');
Return
}
var param = {};
param.home_city = $ (' #query_role_region1 '). ComboBox (' GetValue ');
Param.home_county = $ (' #query_role_region2 '). ComboBox (' GetValue ');
param.role_id = $ (' #query_role_id '). Val ();
Param.role_name = $ (' #query_role_name '). Val ();
param.user_id = user_id;
Param.is_export = "true";
$ (' #maskDiv '). Mask ({
Maskmsg: ' Exporting ... Please wait a moment ... '
});
Window.location.href = ' pri_user_grant_exportrole.jsf? ' +$.param (param);
}
JSP download page
Copy Code code as follows:
<% @page import= "Java.io.OutputStream"%>
<% @page import= "Java.io.PrintWriter"%>
<% @page import= "Java.io.FileNotFoundException"%>
<% @page import= "Java.io.File"%>
<% @page import= "Java.io.FileInputStream"%>
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%
Opens flow information for the specified file
String fileName = "58918-2-import_template.xls";
String filepath = Request.getrealpath ("bassdqm/sqlcheck/template/" +filename);
SYSTEM.OUT.PRINTLN (filepath);
FileInputStream fs = null;
try {
FS = new FileInputStream (new File (filepath));
}catch (FileNotFoundException e) {
E.printstacktrace ();
Return
}
Set response headers and save file names
Response.reset ();
Response.setcontenttype ("application/vnd.ms-excel");
Response.setheader ("content-disposition", "inline; Filename=\ "" + fileName + "\");
Write out flow information
int b = 0;
try {
OutputStream Ops = Response.getoutputstream ();
while ((B=fs.read ())!=-1) {
Ops.write (b);
}
Fs.close ();
Out.clear ();
out = Pagecontext.pushbody ();
}catch (Exception e) {
E.printstacktrace ();
System.out.println ("Download file failed!");
}
%>