jsp下载
<%@ page language="java" contentType="application/x-msdownload" pageEncoding="gbk"%>
<%@page import="java.net.*" %>
<%@page import="java.io.*" %>
<html>
<head>
<title>文件下载</title>
</head>
<body>
<%
//使用文件流输出的方式下载
response.reset(); //取消文件开始的空白行
response.setContentType("application/x-download"); //设置mime类型
String fileName = (String)request.getSession().getServletContext().getRealPath("upload/20111220.xls"); //取得下载文件的绝对路径+文件名
String fileDisplay = "员工信息表.xls"; //下载名称描述
fileDisplay = URLEncoder.encode(fileDisplay,"UTF-8"); //转码
response.setHeader("Content-Disposition","attachment;filename="+fileDisplay);
OutputStream os = null;
FileInputStream is = null;
try{
os = response.getOutputStream();
is = new FileInputStream(fileName);
byte[] bt = new byte[1024];
int i=0;
while((i=is.read(bt))>0){
os.write(bt,0,i);
}
os.flush();
out.clear();
out=pageContext.pushBody();
}catch(Exception e){
e.printStackTrace();
}finally{
if(is!=null){
is.close();
is = null;
}
if(os!=null){
os.close();
os= null;
}
}
%>
</body>
</html>
<%@page import="java.net.*" %>
<%@page import="java.io.*" %>
<html>
<head>
<title>文件下载</title>
</head>
<body>
<%
//使用文件流输出的方式下载
response.reset(); //取消文件开始的空白行
response.setContentType("application/x-download"); //设置mime类型
String fileName = (String)request.getSession().getServletContext().getRealPath("upload/20111220.xls"); //取得下载文件的绝对路径+文件名
String fileDisplay = "员工信息表.xls"; //下载名称描述
fileDisplay = URLEncoder.encode(fileDisplay,"UTF-8"); //转码
response.setHeader("Content-Disposition","attachment;filename="+fileDisplay);
OutputStream os = null;
FileInputStream is = null;
try{
os = response.getOutputStream();
is = new FileInputStream(fileName);
byte[] bt = new byte[1024];
int i=0;
while((i=is.read(bt))>0){
os.write(bt,0,i);
}
os.flush();
out.clear();
out=pageContext.pushBody();
}catch(Exception e){
e.printStackTrace();
}finally{
if(is!=null){
is.close();
is = null;
}
if(os!=null){
os.close();
os= null;
}
}
%>
</body>
</html>