Servlet下载完成后如何刷新页面?Cannot call sendRedirect() after the response has been commit

wx198977 2012-08-22 04:32:23

public void downLoad(String filePath, HttpServletResponse response,
boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;

response.reset();
if (isOnLine) {
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition",
"inline; filename=" + f.getName());

} else {
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}





以上为提交下载的方法,希望在下载后,页面能刷新完成跳转,可是当发起response.sendRedirect();后出现异常
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
请问如何解决?
...全文
320 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
scbb 2012-08-22
  • 打赏
  • 举报
回复
Struts吗? execute最后return null试试看。
菖蒲老先生 2012-08-22
  • 打赏
  • 举报
回复
这个好像挺难做到的,因为下载用的是response请求,根本就刷新不了页面。。。

62,621

社区成员

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

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