/* 把下面所有贴到一个jsp页面,使点击“下载”的按钮连接到这个页面,传入文件名 */
<%@ page contentType="text/html;charset=GB2312" %>
<P>点击超链接下载Zip文件</P>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>
<html>
<BODY>
<%
OutputStream outps = null;
FileInputStream inputs =null;
PrintWriter printwt;
try{
String filePath = request.getParameter("filePath");
outps=response.getOutputStream();
//输出文件用的字节数组,每次发送1024个字节到输出流:
byte bt[]=new byte[1024];
//下载的文件:
File fileLoad=new File(filePath);
//客户使用保存文件的对话框:
response.setHeader("Content-disposition","attachment;filename="+filePath);
//通知用户文件的MIME类型:
response.setContentType("application/txt");
//通知文件的长度:
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
//读取文件*.zip,并发送给客户下载:
inputs=new FileInputStream(fileLoad);
int len=0;
while((len=inputs.read(bt))!=-1){
outps.write(bt,0,len);
}
}catch(Exception e){
response.sendRedirect("downLoadError.jsp");
}finally{
//关闭流
if(inputs!=null) inputs.close();
if(outps!=null) outps.close();
out.clear();
out = pageContext.pushBody();
}
%>
</Body>
</HTML>
/* 下面是异常的 downLoadError.jsp*/
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://eaptag2.suntek.com" prefix="ccf"%>
<%
String path = request.getContextPath();
%>
<html>
<head>
<title>操作日志查询</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
</head>
<script type="text/javascript" src="/eaptag2/js/ccftag.zdialog.js" ></script>
<script type="text/javascript" src="/eaptag2/js/ccftag.form.js"></script>
<script type="text/javascript" src="/eaptag2/js/ccftag.datepick.js"></script>
<script type="text/javascript" src="/eaptag2/js/ccftag.textbox.js"></script>
<script>
</script>
<body scroll=no>
<table align="center">
<tr><td> </td></tr>
<tr><td><font color="red">对不起,在指定目录下找不到下载文件,无法下载!</font></td>
</tr><tr>
<td align = "center"><input type="button" size="30" value="返回" onclick="history.back();"></td>
</tr>
</table>
</body>
</html>
实例: 下载工程中的 META-INF下的corp_template.txt 到本地指定位置。
下载按钮 的链接 连到下边的页面 传入corp_template.txt (文件名称) 异常信息跟上边的异常页面相同
<%@ page contentType="text/html;charset=GB2312" %>
<%@page import="java.io.*" %>
<P>点击超链接下载Zip文件</P>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>
<html>
<BODY>
<%
OutputStream outps = null;
InputStream inputs =null;
PrintWriter printwt;
try{
String filename = request.getParameter("filename");
outps=response.getOutputStream();
//输出文件用的字节数组,每次发送1024个字节到输出流:
byte bt[]=new byte[1024];
//下载的文件:
inputs = getClass().getResourceAsStream("/META-INF/"+ filename );
//客户使用保存文件的对话框:
response.setHeader("Content-disposition","attachment;filename=" + filename);
//通知用户文件的MIME类型:
response.setContentType("application/txt");
//读取文件*.zip,并发送给客户下载:
int len=0;
while((len=inputs.read(bt))!=-1){
outps.write(bt,0,len);
}
}catch(Exception e){
response.sendRedirect("downLoadError.jsp");
}finally{
//关闭流
if(inputs!=null) inputs.close();
if(outps!=null) outps.close();
out.clear();
out = pageContext.pushBody();
}
%>
</Body>
</HTML>