<%@ page language="java" import="java.io.*,java.net.URLEncoder" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'file.jsp' starting page</title>
</head>
<%
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
String filedownload = "给客户端提供的下载文件物理路径";
String filedisplay = "下载文件的文件名";
filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
outp.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
if(outp != null)
{
outp.close();
outp = null;
}
}
%>
<body></body></html>