配置模板路径
@Value("${jeecg.path.template}")
private String excelTemplatePath;
@RequestMapping(value = "/download")
public String download(HttpServletResponse response) throws Exception{
String downloadFilePath = excelTemplatePath + "/train_template.xls";//被下载的文件在服务器中的路径,
// String downloadFilePath = "C:\\Users\\Administrator\\Desktop\\培训辅导导入模板.xls";//被下载的文件在服务器中的路径,
String fileName = "train_template.xls";//被下载文件的名称
File file = new File(downloadFilePath);
if (file.exists()) {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(downloadFilePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
}
return "下载失败";
}