java—将数据写入csv/xlsx会消耗更多内存,并且在处理请求后不会进行清理

z9smfwbn  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(260)

我在spring boot中有一个用java编写的restapi。这将创建一个csv或excel文件,并从数据库中向其中添加记录。但在完成之后,记忆并没有被清除。为1mb文件占用更多内存,之后不再清理内存。代码中是否有任何泄漏,或者如何更改代码以有效地使用内存?

--CSV
public void getCompleteCsvReport(HttpServletResponse response)
      throws Exception {
    List<ReportRecord> reportRecords = new ArrayList<>();
    List<CompletedDetail> completedRecods =
        repository.findCompletedRecords();
    CompletedReportRecord defaultHeader = new CompletedReportRecord();
    if (completedRecods == null || completedRecods.size() == 0)
      reportRecords.add(defaultHeader);
    if (completedRecods != null && completedRecods.size() > 0)
      getPaginatedCompleteCsvReport reportRecords);
    // set file name and content type
    Date date = new Date();
    String filename =
        "Report-" + Constants.CURRENT_DATE_FORMAT.format(date) + ".csv";
    response.setContentType("text/csv");
    response.setHeader(
        HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
    // create a csv writer
    PrintWriter responseWriter = response.getWriter();
    StatefulBeanToCsv<CompletedReportRecord> writer =
        new StatefulBeanToCsvBuilder<CompletedReportRecord>(responseWriter)
            .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
            .withOrderedResults(true)
            .build();
    // write all records to csv file
    writer.write(reportRecords);
    responseWriter.close();
  }

--excel
private void getExcelReportForStream()
    throws Exception {
 try (XSSFWorkbook workbook = new XSSFWorkbook(); ) {
      XSSFSheet sheet = workbook.createSheet("sample Deposits");
      Row headerRow = sheet.createRow(0);
      CellStyle headerCellStyle = workbook.createCellStyle();
      headerCellStyle.setDataFormat(workbook.createDataFormat().getFormat("text"));
      List<String> headers = Constants.FILE_HEADERS;
      for (int col = 0; col < headers.size(); col++) {
        Cell cell = headerRow.createCell(col);
        cell.setCellType(CellType.STRING);
        cell.setCellValue(headers.get(col));
        cell.setCellStyle(headerCellStyle);
      }
      getPaginatedCompleteExcelReport( sheet, 0, 1);
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      workbook.write(outputStream);
      ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
      outputStream.close();
      workbook.close();
      String filename = "Report-";
  Date date = new Date();
  String filename =
      reportName + sConstants.CURRENT_DATE_FORMAT.format(date) + ".xlsx";
  response.setContentType("application/octet-stream");
  response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
  // Copy the stream to the response's output stream.
  ServletOutputStream responseOutputStream = response.getOutputStream();
  IOUtils.copy(inputStream, responseOutputStream);
  inputStream.close();
  responseOutputStream.close();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题