Home > Enterprise >  Problem With outputStream after 1 download
Problem With outputStream after 1 download

Time:11-14

After downloading an Excel with HttpServletResponse the second time i tried to download it dosent work.

           public void DatosaExcel(HttpServletResponse response) throws IOException {
        
                writeHeaderLine();
                writeDataLines();
                 
                ServletOutputStream outputStream = response.getOutputStream();
                workbook.write(outputStream);   
                outputStream.close();

           }
    @GetMapping("/DescargarExcelEmpleados")
    public void DescargarEmpleados(HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        String headerKey = "Content-Disposition";
        String headerValue = "attachment; filename=empleados.xlsx";
        response.setHeader(headerKey, headerValue);
        productService.DatosaExcel(response);
    }

those are my Ctrl and function that calls the excel generator but i cant find a way of solving this issue

CodePudding user response:

I'm guessing you are using Apache POI for Excel work here and also guess that nothing happens when you try to generate it the second time (if there is an error, please provide the log).

This is probably because you didn't close the workbook after write. Always

workbook.write(outputStream);
workbook.close(); //<-- Important!!!

CodePudding user response:

I found the answer i have the workbook declaration XSSFWorkbook workbook = new XSSFWorkbook();, on the service class, so after 1 download the object was already full. so a work aroundi used was in the controller function call i initialized the object again.

workbook = new XSSFWorkbook();

  • Related