@RequestMapping(params = "download")
public void download(HttpServletRequest request, HttpServletResponse response){
OutputStream out = null;
//获取在src/main/resources下的文件
InputStream in = this.getClass().getResourceAsStream("/export/案件导入模版.xls");
//System.out.println(in.toString());
try {
out =response.getOutputStream();//获取输出流
String fileName = "案件导入模版.xls";//下载的文件名
// 解决火狐下载文件名乱码
String agent1 = request.getHeader("USER-AGENT");
if (agent1 != null && agent1.indexOf("MSIE") == -1) {// FF
String newFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8"))))
+ "?=";
response.setHeader("Content-Disposition", "attachment; filename=" + newFileName);
} else { // IE
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
}
//文件下载流处理
byte[] buffer = new byte[10240];
int length = 0;
while ((length = in.read(buffer)) > 0) {
System.out.println("ddd");
out.write(buffer, 0, length);
}
//刷新
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
springmvc文件下载示例代码
最新推荐文章于 2022-11-04 07:00:00 发布