java附件下载
/*
* 附件下载
*/
@RequestMapping("/download")
public void downloadPost(HttpServletResponse response,HttpServletRequest request){
BufferedOutputStream bos = null;
try {
InputStream is = AttendanceController.class.getClassLoader()
.getResourceAsStream("/kaoqin.csv");
String disposition = "attachment;filename="
+ URLEncoder.encode("考勤模板.csv", "UTF-8");//注意(1)
response.setContentType("application/x-msdownload;charset=UTF-8");//注意(2) text/plain
response.setHeader("Content-disposition", disposition);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2048];
while (is.read(buffer) != -1) {
bos.write(buffer);
//bos.flush();
}
bos.close();
is.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
---------------------------------
csv文件在src目录下
----------------------------------
<a href="${path}/routine/attendance/index/download">模板下载</a>