我有REST API,该方法调用一种检索CSV的方法,因为csv可能有数百万行,我一次要检索5万行,我希望在收到第一个查询结果后立即开始下载,然后继续写入同一文件。
我已经编写了以下代码,但是仍然只有在检索到所有结果之后才开始文件下载。
@GET
@Path("network/all/stats/csv/download")
@Produces({"text/csv"})
public Response downloadCSV(UIAffiliateRequest uiInstallsRequest) {StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream os) throws IOException, WebApplicationException {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
int off=0;
try {
CSVResponseType rs;
do {
rs = installsDao.getCSVResult(uiInstallsRequest, off);
String lines[] = rs.getFileName().split("\\r?\\n");
for (String path : lines) {
writer.write(path.toString() + "\n");
}
writer.flush();
off+=50000;
}
while (rs.isEmpty());
}
catch (DaoException e) {
ResponseUtils.getDataAccessErrorResponse(e);
}
}
};
return Response.ok(stream).header("Content-Disposition", "attachment;filename=resp.csv").build();
}