This code only works with 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);
}
}
I have tried Chrome and Opera browsers but all of those are throwing a
Download error. The `CSVFunction` takes a bit of time to finish,
processing around 30K rows. I wonder why Firefox can handle this
properly and while Chrome and Opera cannot. The only time this works
with Chrome is if I force the row processing to say 200 rows only (fast)
then it works and returns the CSV generated.
What could be wrong here? Is there a workaround that this could work
with Chrome?
*The CSVFunction is basically based on
https://www.mkyong.com/java/how-to-export-data-to-csv-file-java/*