通过生成File
,达到下载文件的目的
public R exportCsv(@RequestBody String[] ids, HttpServletResponse response) {
logger.info("导出");
try {
File file = xxxService.exportCsv(ids);
// 设置Content-Disposition
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
InputStream in = new FileInputStream(file.getPath());
OutputStream out = response.getOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.close();
in.close();
// 删除生成的文件
xxxService.deleteFile(file);
} catch (Exception e) {
e.printStackTrace();
logger.error("导出文件失败", e);
}
return null;
}
网友评论