代码
@ApiOperation(value = "下载模板接口")标签:docx,记录,sos,catch,new,null,上传,response From: https://www.cnblogs.com/shengguangshouhu/p/16791486.html
@GetMapping(value = "downloadTemplate")
public void downloadTemplate(HttpServletResponse response) throws IOException {
InputStream inputStream= null;
FileInputStream fis = null;
ServletOutputStream sos = null;
try {
ClassPathResource resource = new ClassPathResource("template.docx");
try {
inputStream = resource.getInputStream();
}catch (Exception e){
throw new ExcelCommonException("无法找到文件");
}
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("template.docx", "UTF-8"));
sos = response.getOutputStream();
IOUtils.copy(resource.getInputStream(), sos);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("下载失败!");
} finally {
try {
if (fis != null) {
fis.close();
}
if (sos != null) {
sos.flush();
sos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}