java.lang.IllegalStateException:
2015-11-24 22:13
513 查看
java.lang.IllegalStateException: getOutputStream() has already been called for this response;
该错误信息一般会在文件下载过程中出现,主要是因为ServletResponse.getOutputStream()方法和public java.io.PrintWriter getWriter()两个方法发生冲突,其中的一个方法被调用了,再调用另一个方法就会抛出异常。
Servlet的API中解释为:
解决方法:在使用完response.getOutputStream()的后面加上两句:
out.clear();//清空缓存的内容,此处out为JSPWriter 对象。
out = pageContext.pushBody();
·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)
·保存JspWriter实例的对象out
·更新PageContext的out属性的内容
该错误信息一般会在文件下载过程中出现,主要是因为ServletResponse.getOutputStream()方法和public java.io.PrintWriter getWriter()两个方法发生冲突,其中的一个方法被调用了,再调用另一个方法就会抛出异常。
Servlet的API中解释为:
public java.io.PrintWriter getWriter() throws java.io.IOException Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1. Calling flush() on the PrintWriter commits the response. Either this method or getOutputStream() may be called to write the body, not both. Returns: a PrintWriter object that can return character data to the client Throws: UnsupportedEncodingException - if the character encoding returned by getCharacterEncoding cannot be used java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object java.io.IOException - if an input or output exception occurred See Also: getOutputStream(), setCharacterEncoding(java.lang.String)
解决方法:在使用完response.getOutputStream()的后面加上两句:
out.clear();//清空缓存的内容,此处out为JSPWriter 对象。
out = pageContext.pushBody();
·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)
·保存JspWriter实例的对象out
·更新PageContext的out属性的内容
public BodyContent pushBody() Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext. Returns: the new BodyContent
相关文章推荐
- javaSE(02)
- 第二次spring冲刺第1天
- 2015年Java代码之02
- 2015年Java代码之01
- JAVA二叉树的创建以及各种功能的实现
- Myeclipse 10 Maven 构建 Java Web 项目
- Java邮件开发---基本概念
- Mockito实现原理探析 -- Mockito.when(...).thenReturn(...)的一个简化实现
- SpringMVC介绍之Validation
- java中堆栈和队列的实现方式
- Struts2框架下表单数据的流向以及映射关系
- java连接neo4j
- Java web的几种异常处理 (转)
- Java NIO教程(下)
- Java NIO教程(中)
- JavaIO和JavaNIO
- Java NIO教程(上)
- 恶补Java(十四)——Java枚举
- 从零开始写javaweb框架笔记1-使用IDEA创建Maven项目
- java 基础 07