此代码仅适用于Firefox:
@Override
public void run(Representation entity) {
HttpServletResponse httpServletResponse = null;
try {
httpServletResponse = ServletUtils.getResponse(getResponse());
OutputStream buffOs = new BufferedOutputStream(httpServletResponse.getOutputStream());
CountingOutputStream countingOutputStream = new CountingOutputStream(buffOs);
OutputStreamWriter outputWriter = new OutputStreamWriter(countingOutputStream,"UTF-8");
httpServletResponse.setContentType("text/csv;charset=utf-8");
httpServletResponse.setHeader("Transfer-Encoding", "Chunked");
httpServletResponse.setHeader("Content-Description", "File Transfer");
httpServletResponse.setHeader("Content-Disposition",
"attachment;filename=\"" + FILE_NAME + ".csv\"");
CSVFunction method = new CSVFunction(outputWriter);
System.out.println("TOTAL BYTES: " + countingOutputStream.getCount());
httpServletResponse.setHeader("Content-Length", countingOutputStream.getCount() + "");
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getOutputStream().flush();
httpServletResponse.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
我尝试过使用Chrome浏览器和Opera浏览器,但所有这些浏览器都出现了下载错误。 CSVFunction
需要一些时间才能完成,处理大约30K行。我想知道为什么Firefox可以正确处理这个问题,而Chrome和Opera则不能。唯一适用于Chrome的情况是,如果我强制行处理只说200行(快速),那么它会起作用并返回生成的CSV。
这里有什么问题?是否有解决方法可以与Chrome配合使用?
CSVFunction基本上基于https://www.mkyong.com/java/how-to-export-data-to-csv-file-java/