1. 提前编辑好的文件放在项目中的某个文件夹中
2. 下载
设置响应头
/**
* 下载导入模板
* @param response
* @param redirectAttributes
* @return
*/
@RequiresPermissions("znyk:etExamResult:view")
@RequestMapping(value = "import/template")
public String importFileTemplate(HttpServletRequest request,HttpServletResponse response, RedirectAttributes redirectAttributes) {
String path = request.getSession().getServletContext().getRealPath("/static/excelModel");
System.out.println(path);
String fileName = "成绩导入模板.xlsx";
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data");
try {
response.setHeader("content-Disposition", "attachment;fileName="+URLEncoder.encode(fileName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(path, fileName);
try {
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024];
int index = 0;
while((index = is.read(b)) != -1) {
os.write(b, 0, index);
os.flush();
}
os.close();
is.close();
return null;
} catch (FileNotFoundException e) {
e.printStackTrace();
addMessage(redirectAttributes, "导入模板下载失败!失败信息:"+e.getMessage());
} catch (IOException e) {
e.printStackTrace();
addMessage(redirectAttributes, "导入模板下载失败!失败信息:"+e.getMessage());
}
return "redirect:"+Global.getAdminPath()+"/****/etExamResult/?repage";
}