response.getOutputStream()出错处理

本文介绍了在JSP编程中如何解决使用response.getOutputStream()与response.getWriter()产生的冲突问题,并提供了具体的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写JSP程序的时候,如果程序中调用了response.getOutputStream()去向客户端输出文件等数据流,容器就会抛出这样的异常:
Java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:596)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:186)
  产生这样的异常原因:是web容器生成的servlet代码中有out.write(""),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规范说明,不能既调用response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出IllegalStateException,因为在jsp中,out变量实际上是通过response.getWriter得到的,你的程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。
  解决方案:在程序的最后添加:
out.clear();
out = pageContext.pushBody();
就可以了
public static void exportMillCertificateWord(HttpServletResponse response, Map<String, Object> map, String title, String ftlFile) throws IOException { System.out.println("正在导出" + title); ServletOutputStream out = null; try { response.setCharacterEncoding("utf-8"); response.setContentType("application/msword"); // 设置浏览器以下载的方式,处理该文件名 String fileName = title+ getDataStr() + ".docx"; response.setHeader("Content-Disposition", "attachment;filename=" .concat(String.valueOf(URLEncoder.encode(fileName, StandardCharsets.UTF_8)))); out = response.getOutputStream(); out.write(generateWord(map, title, ftlFile)); }catch (Exception ex){ log.error("发生了未具体分类的异常:", ex); }finally { if(out != null) out.close(); //关闭对应流 } }这个我可以下载docx,为啥我使用 public static byte[] wordToPdf(byte[] wordBytes) throws IOException { InputStream doc = null; ByteArrayOutputStream out = null; try { doc = new ByteArrayInputStream(wordBytes); XWPFDocument document; document = new XWPFDocument(doc); PdfOptions options = PdfOptions.create(); out = new ByteArrayOutputStream(); PdfConverter.getInstance().convert(document, out, options); } catch (IOException e) { throw new RuntimeException(e); } finally { if (doc != null) { doc.close(); } if (out != null) { out.close(); } } return out.toByteArray(); }将wordbyte[]转为pdfByte[],然后下载pdf public static void exportPdf1(HttpServletResponse response, Map<String, Object> map, String title, String ftlFile) { System.out.println("正在导出" + title); try (ServletOutputStream os = response.getOutputStream()) { byte[] wordBytes = generateWord(map, title, ftlFile); byte[] pdfBytes = wordToPdf(wordBytes); response.setCharacterEncoding("utf-8"); response.setContentType("application/pdf"); // 设置浏览器以下载的方式,处理该文件名 String fileName = title + getDataStr() + ".pdf"; response.setHeader("Content-Disposition", "attachment;filename=" .concat(String.valueOf(URLEncoder.encode(fileName, StandardCharsets.UTF_8)))); os.write(pdfBytes); } catch (IOException e) { log.error("生成pdf出错:", e); } }的时候提示 ERROR c.p.w.c.e.DefaultExceptionHandler - [handleException,28] - org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: The supplied data appears to be a raw XML file. Formats such as Office 2003 XML are not supported
03-08
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值