//获得响应客户端的输出流
OutputStream o = response.getOutputStream();
//缓冲区字节数组,每次发送500字节到输出流
byte[] b = new byte[500];
//要下载的文件
String fileName = "test.rar";
String filePath = "E:/Downloads/";
File file = new File(filePath, fileName);
//客户端要保存文件的对话框
response.setHeader("Content-disposition","attachment;filename="+fileName);
//通知客户文件mime的类型
response.setContentType("application/x-tar");
//通知客户文件的长度
long len=file.length();
//response.setContentLength(len);
String strLen=String.valueOf(len);
response.setHeader("Content_Length",strLen);
//读文件并发给用户下载
FileInputStream in=new FileInputStream(file);
int n=0;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
in.close();
o.close();
原文地址:http://www.cnblogs.com/beast-king/p/3923185.html