ITeye博客

getOutputStream() has already been called for this

eaglyang · 2008-12-19 09:31 · JAVA应用 · Markdown

最近在做MIS接口,发现有这这样的问题: getOutputStream() has already been called for this response异常 到网上查了一下,解释是这样的: 在tomcat5下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。具体的原因就是在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后有一段这样的代码 finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } 这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和 response.getOutputStream()相冲突的!所以会出现以上这个异常。

解决方法:在使用完输出流以后调用以下两行代码即可: out.clear(); out = pageContext.pushBody();

以下是我自己做的一个例子: <% //MIS系统接收的数据 BufferedReader freader = new BufferedReader(new InputStreamReader((InputStream)request.getInputStream(), "UTF-8")); StringBuffer sb1 = new StringBuffer(1024); String str; while((str = freader.readLine()) != null) { sb1.append(str); sb1.append("\n"); } System.out.println("MIS系统接收的数据:" + sb1.toString());

//MIS系统返回的数据
String filePath = request.getSession().getServletContext().getRealPath("/");
String xmlFilePath = filePath + XmlConstants.XML_IMPORT_OUTPUT;

String content = XmlUtil.getDoucumentByXml(xmlFilePath);
response.getOutputStream().write(content.getBytes(XmlConstants.UTF_ENCODING));
out.clear();
out = pageContext.pushBody();

%>

加上以后就OK了,虽然这方面做的比较少,但是把问题解决了心里还是很爽的!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。