nutzam/nutz — Issue #129: 实现一个下载的功能,功能可以正常使用,但后台报错
Author: goulin
Created: 2012-01-16 12:09:56+00:00
State: open

报错如下:
2012-1-16 19:55:03 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [nutz] in context with path [/airflow] threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:339)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at org.nutz.mvc.view.ForwardView.render(ForwardView.java:64)
at org.nutz.mvc.impl.processor.ViewProcessor.process(ViewProcessor.java:35)
at org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:41)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:66)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:30)
at org.nutz.mvc.NutServlet.service(NutServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
2012-1-16 20:01:31 org.apache.catalina.core.StandardContext reload
信息: Reloading Context with name [/airflow] has started

前台是Extjs4, 提交一个ajax,请求action中的download方法 :

@At
public void download(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {

String root = request.getSession().getServletContext().getRealPath("/");
File file = new File(root+"WEB-INF/classes/代维数据模板.xls");
if(file.exists()){
System.out.println("存在");
}
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ new String(file.getName().getBytes("gb2312"),"iso8859-1") + "\""); //可以解决中文乱码问题
OutputStream os =null;
FileInputStream fis = null;
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
byte[] b = new byte[(int) file.length()];
int i = 0;
while ((i = fis.read(b)) > 0) {
os.write(b, 0, i);
}
System.out.println("-- os flush ---");
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}