我正在尝试将XLSX文件发送到客户端。一切正常,但是提交响应后出现了异常java.lang.IllegalStateException: Can not call sendRedirect()
。当文件已经发送到客户端时,将抛出该错误。请注意,该文件已在此代码段之前创建。
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";
}