/*
* 下载客户档案模板
* @param response
*/
@ApiOperation(value = "下载客户档案模板", notes = "下载客户档案模板")
@PostMapping(value = "/downloadCustomerTemplate")
public void downloadCustomerTemplate(HttpServletResponse response) {
fis = null;
try {
String filePath = "C:/test/customer_excel_imp.xls";
File file = new File(filePath);
if (!file.exists()) {
response.sendError(404, "File not found!");
} else {
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
// 清空response
fis.read(buffer);
// JSONP 解决跨域问题
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
// 设置response的Header
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"iso-8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
fis.close();
}
} catch (IOException ex) {
logger.error("异常:",ex.getMessage());
}
}