1. jsp页面使用window.location = url+"?"+params;来请求导出api
2.在后台方法中,设置导出名与内容与文件名编码格式
response.setContentType("application/octet-stream");
response.setCharacterEncoding("GBK");
try {
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("XXX.csv", "UTF-8"));
} catch (UnsupportedEncodingException e) {
logger.error("encode not support, Message: {}", e.getMessage());
}
String header = "XXX,XXX,XXX";
3.一行行写
PrintWriter writer;
try {
writer.println(header);
for (Crime crime : results) {
String line = String.format("%s,%s,%s", XXX, XXX, XXX,);
writer.println(line);
}
writer.flush();
writer.close();
} catch (IOException e) {
logger.error("Generate CSV, Message: {}", e.getMessage());
}