Sometimes a template can be downloaded. This simple download server already has the file function, which can be easily implemented through jsp file download.
// Jsp page js
Copy codeThe Code is as follows:
/**
* Export a role
*/
Function exportRole (){
Var user_id = $ ('input [name = userListRadio]: checked'). attr ('id ');
If (! User_id | user_id = ''){
Showinfo ('select a 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 ...'
});
Window. location. href = 'pri _ user_grant_exportRole.jsf? '+ $. Param (param );
}
// Jsp Download Page
Copy codeThe Code is 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" %>
<%
// Open the stream information of 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 the response header and save the file name
Response. reset ();
Response. setContentType ("application/vnd. ms-excel ");
Response. setHeader ("Content-Disposition", "inline; filename = \" "+ fileName + "\"");
// Write the stream 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 ("An error occurred while downloading the file! ");
}
%>