?1
public
?
void
?doDownLoad(HttpServletRequest?request,?HttpServletResponse?response,?
?2
????????????String?absolutePath)?
{
?3
????????
?4
????????
//
設置響應頭信息
?5
????????response.setContentType(
"
application/octet-stream;charset=UTF-8
"
);?
?6
????????log.debug(
"
GET:?
"
?
+
?absolutePath);
?7
????????
?8
????????String?str?
=
?FilePathParseUtil.getFileNameByPath(absolutePath);
?9
????????
//
調用自定義的編碼函數,解決不同瀏覽器上對漢字編碼的處理
10
????????str?
=
?
this
.encodeFileName(request,?str)
==
null
?
str:
this
.encodeFileName(request,?str);
11
????????
//
設置response頭信息,從而顯示正確的文件名,并彈出另存對話框
12
????????response.setHeader(
"
Content-Disposition
"
,?
"
attachment;?filename=
"
?
13
????????????????
+
?str);
14
????????OutputStream?out?
=
?
null
;
15
????????
try
{
16
????????????
//
從response得到輸出流,從而向客戶端寫出文件
17
????????????out?
=
?response.getOutputStream();
18
????????}
catch
(IOException?e)
{
19
????????????log.error(
"
output?stream?is?null
"
);
20
????????????e.printStackTrace();
21
????????}
22
????????
this
.doDownLoad(out,?absolutePath);
23
????}
24
????
25
????
/**?*/
/**
26
?????*?根據不同瀏覽器對文件名進行編碼
27
?????*?
@param
?request?客戶端請求
28
?????*?
@param
?fileName?文件名
29
?????*?
@return
?編碼后的文件名
30
?????
*/
31
????
public
?String?encodeFileName(HttpServletRequest?request,?String?fileName)
{???
32
????????String?agent?
=
?request.getHeader(
"
USER-AGENT
"
);
33
????????
try
{
34
????????????
if
?(
null
?
!=
?agent?
&&
?
-
1
?
!=
?agent.indexOf(
"
MSIE
"
))?
{???
35
????????????????
return
?URLEncoder.encode(fileName,?
"
UTF-8
"
);???
36
????????????}
else
?
if
?(
null
?
!=
?agent?
&&
?
-
1
?
!=
?agent.indexOf(
"
Mozilla
"
))?
{???
37
????????????????
return
?
"
=?UTF-8?B?
"
+
38
????????????????????????
new
?String(
39
????????????????????????????????Base64.encodeBase64(
40
????????????????????????????????????fileName.getBytes(
"
UTF-8
"
)
41
????????????????????????????????)
42
????????????????????????)?
+
?
"
?=
"
;???
43
????????????}
?
else
?
{???
44
????????????????
return
?fileName;???
45
????????????}
46
????????}
catch
(UnsupportedEncodingException?e)
{
47
????????????
return
?
null
;
48
????????}
49
????}
??

?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
