@GetMapping("/template")
public ResponseEntity<byte[]> downloadImportTemplate(Integer type) {
String fileName;
if (type == 1) {
fileName = "importDriverTemplate.xlsx";
} else {
fileName = "importCarTemplate.xlsx";
}
InputStream in = this.getClass().getResourceAsStream("/template/" + fileName);
HttpHeaders headers = new HttpHeaders();
//下载显示的文件名,解决中文名称乱码问题
try {
headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(fileName, "UTF-8"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
} catch (IOException e) {
log.error("下载模板异常", e);
}
return null;
}
使用spring下载模板,在服务器上各种找不到路径。使用上面的写法可实现。