直接上干货:
@ApiOperation(value = "模板下载", notes = "GET http://{host}:{port}/credit/publicity/config/template/downloadById")
@RequestMapping(method = RequestMethod.GET,path="/template/downloadById")
public ResponseEntity downloadById(HttpServletRequest request, String id){
String filepath ="";
switch (id) {
case "1":
filepath = "/file/template/模板名称.xlsx";
break;
case "2":
filepath = "/file/template/模板名称2.xlsx";
break;
default:
break;
}
ClassPathResource file = new ClassPathResource(filepath);
try {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment;filename="+ new String(file.getFilename().getBytes("utf-8"), "ISO8859-1"));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(file.getInputStream()));
} catch (IOException e) {
logger.error("系统异常:{}",e);
return null;
}
}



