Sample Code for jsp File Download

Source: Internet
Author: User

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! ");
}

%>

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.