@RequestMapping("/downloadCzsc")
public void downloadCzsc(HttpServletRequest request,HttpServletResponse response) {
try {
// 获得请求文件名
/*String filename = request.getParameter("测试文件");*/
// 获取目标文件的绝对路径
String srcFile = request.getServletContext().getRealPath("/download/template/czsc/czsc.doc");
// 设置文件MIME类型
// response.setContentType(getServletContext().getMimeType(filename));
response.setContentType(new MimetypesFileTypeMap().getContentType("czsc.doc"));
// 设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename=" + new String("测试文件".getBytes("GBK"), "ISO-8859-1") + ".doc");
// 读取目标文件
InputStream in = new FileInputStream(srcFile);
//通过response将目标文件写到客户端
OutputStream out = response.getOutputStream();
// 写文件
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
} catch (Exception e) {
try {
response.sendRedirect("/error/error.jsp");
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}