现在的位置: 首页 > 综合 > 正文

JSP上传图片产生 java.io.IOException: Stream closed异常解决方法

2013年04月20日 ⁄ 综合 ⁄ 共 1205字 ⁄ 字号 评论关闭

在做 jsp 上传图片时,把 java 代码直接改成 jsp,上传时产生 如下异常:
2012-12-31 8:59:21 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.io.IOException: Stream closed
...
百思不得其解,翻出 jsp 转成 servlet 后的代码。如下(很很的醒目一下):
复制代码 代码如下:
...
}catch(Exception e){
e.printStackTrace();
}finally{
out.flush(); //
out.close();// 此处为源始代码
DBHelper.freeConnection(connection);
}
out.write('\r'); // 如上我已经关了 out 对象,但此处还在使用,所以便产生了如开始所描述的异常
out.write('\n');
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
...

解决办法:把程序中加红加粗的代码改成:

复制代码 代码如下:
out.flush() ;
out = pageContext.pushBody(); // 关于该段程序的解释,doc中已经说的很清楚。

如下:(要特别注意一下flush()和clear()方法的区别,因为需求不同程序是不同的)

abstract void flush()
Flush the stream.
abstract void clear()
Clear the contents of the buffer.

PageContext 实现了抽象类 JspContext ,方法:pushBody(), 保存当前的out对象

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.

public abstract class BodyContent
extends JspWriter

out 内置对象

抱歉!评论已关闭.