点击一个连接实现简单的文件下载功能。
<a href="<%=path%>/downloadFile.action">下载模板</a>
@RequestMapping(value = "/downloadFile.action")
public String downloads(HttpServletResponse response,HttpServletRequest request) throws Exception {
String fileName = "车辆卡口黑名单模板.xlsx";
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
//InputStream input = new FileInputStream(file);
//文件放在“resources/resourceType/“ 下面
InputStream input = getClass().getResourceAsStream("/resourceType/"+ fileName);
OutputStream out = response.getOutputStream();
byte[] buff = new byte[1024];
int index = 0;
while ((index = input.read(buff)) != -1) {
out.write(buff, 0, index);
out.flush();
}
out.close();
input.close();
return null;
}