getOutputStream() has already been called for this response 异常问题

Smart_Vnote 2012-12-27 11:13:52
我的代码:
try {
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;

response.reset(); // 非常重要
// 纯下载方式
response.setContentType("application/zip");
if (flag) {// 非压缩
response.setHeader("Content-Disposition", "attachment; filename=" + exportName + ".csv");
} else {// 压缩
response.setHeader("Content-Disposition", "attachment; filename=" + exportName + ".zip");
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);

out.flush();
br.close();
out.close();

} catch (Exception e) {
e.printStackTrace();
log.errorOther(null, e, LogErrorConstant.OTHER_UNKNOWN_ERROR);
}
return null;

网上很多网友说,在使用完输出流以后加入下面两行代码:
out.clear();
out = pageContext.pushBody();

但是我的是在Java中写的,不是JSP界面,是在Action中,有没有哪位大侠遇到过这种问题呢?
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
笑莫问 2012-12-27
  • 打赏
  • 举报
回复
br.close(); out.close(); 应该是先关闭out响应输出流,在关闭br吧,lz试试
Smart_Vnote 2012-12-27
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
while ((len = br.read(buf)) > 0) out.write(buf, 0, len); out.flush(); br.close(); out.close(); 你这是写一点刷新一点,然后关闭,再打开?
while ((len = br.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); br.close(); out.close(); 写完了,在刷新,关闭的!我们这个模块是好久前,别人写的了。 还有之前的提交方式是普通提交,因为我现在需要捕获到action有没执行完的一个状态,使用了ajax,然后出了这个问题
  • 打赏
  • 举报
回复
while ((len = br.read(buf)) > 0) out.write(buf, 0, len); out.flush(); br.close(); out.close(); 你这是写一点刷新一点,然后关闭,再打开?
Smart_Vnote 2012-12-27
  • 打赏
  • 举报
回复
问题补充:我的页面调用是使用ajax的方式提交的
Smart_Vnote 2012-12-27
  • 打赏
  • 举报
回复
引用 4 楼 hucp2007 的回复:
br.close(); out.close(); 应该是先关闭out响应输出流,在关闭br吧,lz试试
这个换过来也不好用

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧