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";
}