jsp 文件下載示例代碼
更新時(shí)間:2013年10月29日 10:53:42 作者:
簡單的下載服務(wù)端已存在文件功能,就可以方便的通過jsp文件下載的方式來輕松實(shí)現(xiàn),具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下
有的時(shí)候一個(gè)模板的下載,這種簡單的下載服務(wù)端已存在文件功能,就可以方便的通過jsp文件下載的方式來輕松實(shí)現(xiàn)。
//jsp 頁面 js
/**
* 導(dǎo)出角色
*/
function exportRole(){
var user_id = $('input[name=userListRadio]:checked').attr('id');
if(!user_id ||user_id == ''){
showinfo('請選擇用戶!');
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:'正在導(dǎo)出...請稍后...'
});
window.location.href = 'pri_user_grant_exportRole.jsf?'+$.param(param);
}
//jsp下載頁面
<%@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"%>
<%
//打開指定文件的流信息
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;
}
//設(shè)置響應(yīng)頭和保存文件名
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
//寫出流信息
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("下載文件失敗!");
}
%>

//jsp 頁面 js
復(fù)制代碼 代碼如下:
/**
* 導(dǎo)出角色
*/
function exportRole(){
var user_id = $('input[name=userListRadio]:checked').attr('id');
if(!user_id ||user_id == ''){
showinfo('請選擇用戶!');
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:'正在導(dǎo)出...請稍后...'
});
window.location.href = 'pri_user_grant_exportRole.jsf?'+$.param(param);
}
//jsp下載頁面
復(fù)制代碼 代碼如下:
<%@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"%>
<%
//打開指定文件的流信息
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;
}
//設(shè)置響應(yīng)頭和保存文件名
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
//寫出流信息
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("下載文件失敗!");
}
%>
相關(guān)文章
Apache+Servlet+Jsp環(huán)境設(shè)置(上)
Apache+Servlet+Jsp環(huán)境設(shè)置(上)...2006-10-10JSP頁面文件中base標(biāo)記用法實(shí)例分析
這篇文章主要介紹了JSP頁面文件中base標(biāo)記用法,以實(shí)例形式較為詳細(xì)的分析了JSP中base標(biāo)記的功能與具體使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Java Web項(xiàng)目中連接Access數(shù)據(jù)庫的配置方法
本文是對前幾天的“JDBC連接Access數(shù)據(jù)庫的幾種方式”這篇的升級(jí)。由于在做一些小項(xiàng)目的時(shí)候遇到的問題,因此才決定寫這篇博客的。昨天已經(jīng)將博客發(fā)布了,但是后來經(jīng)過一些驗(yàn)證有點(diǎn)問題,所以今天改了一下重新的發(fā)布了2013-05-05Java動(dòng)態(tài)代理實(shí)現(xiàn)AOP
Java動(dòng)態(tài)代理實(shí)現(xiàn)AOP...2006-10-10使用jsp調(diào)用javabean實(shí)現(xiàn)超簡單網(wǎng)頁計(jì)算器示例
這篇文章主要介紹了使用jsp和javabean實(shí)現(xiàn)超簡單網(wǎng)頁計(jì)算器示例,需要的朋友可以參考下2014-04-04Java Web開發(fā)之圖形驗(yàn)證碼的生成與使用方法
這篇文章主要介紹了Java Web開發(fā)之圖形驗(yàn)證碼的生成與使用方法,較為詳細(xì)的分析了JSP驗(yàn)證碼的實(shí)現(xiàn)原理與生成技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09