outfile.java
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.servlet.bksh.DMC;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class outfile extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 5716236437264937310L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
//得到要下载的文件名称
String filename=request.getParameter("filename");
//文件存放的路径,合成绝对路径
String dir = this.getServletContext().getRealPath("/");
String filepath=dir+"bksh"+"\\"+filename;
//得到这个文件的对象
File file=new File(filepath);
//response的编码方式为.doc下载
response.setContentType("application/msword");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//文件名
response.setHeader("Content-Disposition","attachement;filename=" + new String(filename.getBytes(), "UTF-8"));
//独处文件的IO流
FileInputStream fis=new FileInputStream(file);
@SuppressWarnings("resource")
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();
}
}
/*
* //request.setCharacterEncoding("utf-8");
* //response.setCharacterEncoding("GBK"); String
* filename=request.getParameter("filename");
*
* response.setContentType("image/jpeg");
* //response.setHeader("Content-Type","application/x-excel");
* response.setHeader("Content-Disposition", "attachment; filename=" +
* filename);
*
*
* String dir = this.getServletContext().getRealPath("/"); String
* filepath=dir+"bksh"+"\\"+filename;
*
* File file=new File(filepath); response.setContentLength((int)file.length());
* InputStream fis=new BufferedInputStream(new FileInputStream(file)); byte[]
* buffer = new byte[fis.available()]; fis.read(buffer); fis.close();
* OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
* toClient.write(buffer); toClient.flush(); toClient.close(); //OutputStream
* ops=response.getOutputStream(); //int aRead = 0; //while((aRead = fis.read())
* != -1 & fis != null) //{ //// System.out.println(aRead); // ops.write(aRead);
* //} //ops.flush(); System.out.println(filepath);
*/