新建: ex4_15.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<p> 下载文档 book.zip</p>
<a href="loadFile.jsp"> 下载 book.zip</a>
</body>
</html>
loadFile.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
OutputStream o=response.getOutputStream();
byte b[]=new byte[500];
File fileLoad=new File("d:/test","book.zip");
response.setHeader("Content-disposition","attachment;filename="+"book.zip");
response.setContentType("application/x-tar");
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
FileInputStream in =new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1)
{
o.write(b,0,n);
}
%>
</body>
</html>