日期:2014-05-17 浏览次数:20672 次
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到要下载的文件名称
String filename=request.getParameter("filename");
//文件存放的路径,合成绝对路径
String dir = this.getServletContext().getRealPath("/");
String filepath=dir+"bksh"+"\\"+filename;
//得到这个文件的对象
File f=new File(filepath);
//response的编码方式为.doc下载
response.setContentType("application/msword");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//文件名
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
//独处文件的IO流
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当读文件的缓存
long k=0;//该值用于计算当前实际下载了多少字节
//response对象得到输出流
OutputStream myout=response.getOutputStream();
//开始循环下载
while(k<file.length()){
int j=buff.read(b,0,1024);
k+=j;
//将b中的数据写到客户端的内存
myout.write(b,0,j);
}
//将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
String downloadFileName = "";
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
downloadFileName = URLEncoder.encode(fileRealName, "UTF-8");
} else {
downloadFileName = new String(fi