Java.lang.IllegalStateException:提交响应后无法调用sendRedirect()

时间:2019-04-20 23:51:20

标签: java jsp

我正在尝试将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";
}

0 个答案:

没有答案