0

I'm trying to send an XLSX file to the client. Everything works, but I get the exception java.lang.IllegalStateException: Can not call sendRedirect() after the response has been committed. This is thrown when the file has already been sent to the client. Note that the file has already been created prior to this bit of code.

public String generateExcelCupon(HttpServletRequest request, HttpServletResponse response) {
    try {
        String nameFile = System.getProperty("user.home")+"/Desktop"+request.getParameter("nameFile")+".xlsx";
        XSSFWorkbook workbook = new XSSFWorkbook();

        response.setContentType("application/vnd.ms-excel");
        PrintWriter outPut = response.getWriter();
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=\""
                + request.getParameter("nameFile") + ".xlsx");

        FileInputStream fileInputStream = new FileInputStream(nameFile);

        int i;
        while ((i = fileInputStream.read()) != -1) {
            outPut.write(i);
        }
        fileInputStream.close();
        outPut.close();
        file.delete();
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return "success";
}
Anil
  • 655
  • 1
  • 11
  • 25
Philip
  • 1
  • you have close your `outPut.close();` and then you have send `"success"` ,maybe this is the problem . check [here](https://stackoverflow.com/questions/26352632/how-to-fix-cannot-call-sendredirect-after-the-response-has-been-committed/26352710) – Swati Apr 21 '19 at 05:26
  • I tried to change the method to void, but I keep throwing the exception.. I'm using mvc spring and this exception is released after the controller finishes its execution – Philip Apr 22 '19 at 18:51

0 Answers0