在文件下载的时候 遇到这个问题。
关键代码如下:
         BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
                   fis = new FileInputStream(filePath); bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos); int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
                //在这里出现错误
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
finally {
try { if (bos != null)
bos.close();
if (fos != null)
fos.close();
if (fis != null)
fis.close();
if (bis != null)
bis.close();

} catch (IOException ee) {
throw ee;
}
}
这是什么原因呢,在网上搜了很久,也没有一个好使的办法,麻烦哪位大虾帮忙解答下下。

解决方案 »

  1.   

    应该某处代码用到了writer但是没有关闭,每次用完PrintWriter的时候应该关闭
    如例:
    HttpServletResponse response = ServletActionContext.getResponse();
    PrintWriter writer = response.getWriter();
    writer.close();
      

  2.   

    其他Writer也一样 用完必须要关闭
      

  3.   

    我把writer都关闭后,现在报这个问题:
    java.lang.IllegalStateException: getOutputStream() has already been called for this response。但是我只有在这里一处用到,其他地方都没有用到。为什么还报has already been called?
      

  4.   


    原因是我连续两次使用了上面的程序,但每一次都有关闭。为什么还报has already bean called?
      

  5.   

    加上以下代码:out.clear();
    out=pageContext.pushBody();
      

  6.   


    这个out 指什么,response.getWriter?加在什么位置  程序在 Action里面的。
      

  7.   

    问题终于解决了,感谢1楼。在一楼大虾的提醒下,把writer全部关闭后,系统报ClientAbortExcetion异常。这是因为客户端断开了连接造成的。在系统报ClientAbortExceptions之后接着抛出了response.getOutStream has been called这个错误。这是因为上面的异常造成连接已经中断,其response已经被重置造成的。
    当抛出ClientAbortException的时候,最好检查下客户端是否正常接收文件。我的程序就是因为在传给客户端文件大小的时候出错,造成的这个错误。
    我的点点困难和大家分享,希望对需要的人有帮助。
      

  8.   

    刚遇到此问题, out.clear();//报错 不起作用了