springboot csv 导出百万级别数据
- 引用csv所需要用到的maven包
- 导出数据
- 响应时长应该还是合格的
引用csv所需要用到的maven包
net.sf.supercsv
super-csv
2.4.0
导出数据
@PostMapping("/find-runtime-export-test")
@ApiOperation("运行数据导出")
public void test(HttpServletResponse response) throws IOException {
//附件下载
response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(String.format("%s.csv", System.currentTimeMillis() + "", runtimeData.getUnit()), "UTF-8"));
//获取响应输出流
ServletOutputStream os = null;
try {
os = response.getOutputStream();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 1000000; i++) {
buf.append("name" + i);
buf.append(CSV_COLUMN_SEPARATOR);
buf.append("value" + i);
buf.append(CSV_RN);
}
os.write(buf.toString().getBytes("UTF-8"));
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(os);
}
}
响应时长应该还是合格的