我有一种方法,可以将数据导出到csv文件,当报表的记录数少于100k时,它可以正常工作,当记录数更大时,问题就出现了,我得到了outofmemoryerror。
如您所见,我使用的是supercsv库,下面是我编写的导出到csv的代码。我假设问题是在for循环中试图写入文件,您有什么解决方案来防止outofmemory错误吗?
当做。
public void exportDataToCsv(){
String csvFileName = "report.csv";
response.setContentType("text/csv");
// creates mock data
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
csvFileName);
response.setHeader(headerKey, headerValue);
//list to be exported to csv file
List<Vulnerability> vulnerabilities = vulnerabilityService.getVulnerabilities();
// uses the Super CSV API to generate CSV data from the model data
ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(),
CsvPreference.STANDARD_PREFERENCE);
String[] header = { "id", "Name", "Status", "Owner", "Reader" };
csvWriter.writeHeader(header);
for ( Vulnerability vuln : vulnerabilities) {
csvWriter.write(vuln, header);
}
csvWriter.close();
}
暂无答案!
目前还没有任何答案,快来回答吧!