JSP file download sample code _JSP programming

Source: Internet
Author: User
Tags save file
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!");
}

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