ad472d18f55eb668ff481d4e1e5f9b5479df14fd.svn-base
2.7 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
package com.thinkgem.jeesite.modules.reg.web.helpFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "${adminPath}/reg/helpFileDownload")
public class HelpFileController {
/**
* 打开下载文档列表界面
* @param request
* @param response
* @return
*/
@RequestMapping(value = {"list", ""})
public String list(HttpServletRequest request, HttpServletResponse response) {
return "modules/reg/helpFiles/helpFilesList";
}
/**
* 文档下载方法
* @param request
* @param response
* @param model
* @return
*/
@RequestMapping(value = "download")
public String downloadTemplet(HttpServletRequest request,HttpServletResponse response, Model model){
response.setCharacterEncoding("UTF-8");
String filepath = "D:" + File.separator + "download" + File.separator + "手册汇总.pdf";
if (filepath != null) {
OutputStream os = null;
FileInputStream fis = null;
try {
String file = filepath;
if (!(new File(file)).exists()) {
model.addAttribute("msg", "没有找到下载源文件。");
return "modules/reg/helpFiles/helpFilesList";
}
String filename = file.substring(file.lastIndexOf(File.separator)+1);
//System.out.println("文件名为:"+filename);
os = response.getOutputStream();
response.setHeader("content-disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1"));
response.setContentType("application/octet-stream");//八进制流 与文件类型无关
byte temp[] = new byte[1024];
fis = new FileInputStream(file);
int n = 0;
while ((n = fis.read(temp)) != -1) {
os.write(temp, 0, n);
}
os.flush();
} catch (IOException e) {
model.addAttribute("msg", "出错了,请稍后重试。");
return "modules/reg/helpFiles/helpFilesList";
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
System.out.println("输出流关闭异常");
model.addAttribute("msg", "出错了,请稍后重试。");
return "modules/reg/helpFiles/helpFilesList";
}
}
if (fis != null){
try {
fis.close();
} catch (IOException e) {
System.out.println("输入流关闭异常");
model.addAttribute("msg", "出错了,请稍后重试。");
return "modules/reg/helpFiles/helpFilesList";
}
}
}
}
return "modules/reg/helpFiles/helpFilesList";
}
}