导出
2019-07-31 本文已影响0人
一起DP吧
通过生成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;
}