问题:Cannot forward after response has been committed

倚楼听风雨_ 2011-08-26 12:23:20
先贴代码:
一、Struts1的Action中的一个方法的一段:
ExcelUtils u = new ExcelUtils();
try {
OutputStream outputStream = new BufferedOutputStream(response
.getOutputStream());
u.downLoadExcel(request, all, headName, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
//return null;
request.setAttribute("pageData", lis);
return mapping.findForward("weekStat");
二、这是我要调用ExcelUtils类的 downLoadExcel()方法,方法内容如下:
public void downLoadExcel(HttpServletRequest request,List ls,String[] headName,OutputStream out){
FileInputStream in=null;
File file=null;
String realPath = request.getRealPath("");
String userExcel = realPath + "\\userExcel\\"
+ new Date().getTime() + ".xsl";//获得服务器生成路ᅣ1�7+文件ᅣ1�7
this.outputExcel(userExcel, ls, headName);//生成excel
try {
file=new File(userExcel);
in=new FileInputStream(file);
while(true){
byte[] b=new byte[1024];
if(in.read(b)==-1){
break;
}
out.write(b);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println("下载excel异常");
e1.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
if(out!=null){
out.flush();
out.close();
}
if(file!=null){
file.delete();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}
三、现在的问题是,我这个方法执行完后,并没有return mapping.findForward("weekStat");页面上一直在运行,但实际上操作已经完成了。控制台也报错了,报错信息是:

严重: Servlet.service() for servlet action threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:302)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at com.shareinfo.update.ver40.common.BaseActionServlet.process(BaseActionServlet.java:38)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)

求高手解释
...全文
109 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yexiongMYBH 2011-08-27
  • 打赏
  • 举报
回复
你在下载完毕之后,把response对象给关闭掉了吧,导致跳转不了。。


你完全没必要这么做啊,你可以将这个DownLoadExcel写成一个单独的Action来处理,那样子多好,省事省力。

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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