我需要使用 Struts2 REST 流式传输文件。尽管流式传输正常,但 Web 服务器会引发错误:
异常处理结果。:java.io.IOException: UT010029: Stream is closed
这是动作代码:
public HttpHeaders index() {
try {
// ...
response.setContentType(vo.getContentType());
response.setContentLength(bytes.length);
response.addHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
response.getOutputStream().write(bytes);
} catch( Exception e ) {
log.error(e);
}
return new DefaultHttpHeaders("index");
}
网络服务器日志:
13-04-2018 16:01:28,335 INFO [org.apache.struts2.rest.RestActionInvocation] (default task-15) Executed action [//api/blablaAction!index!json!200] took 208 ms (execution: 16 ms, result: 192 ms)
13-04-2018 16:02:08,927 ERROR [org.apache.struts2.rest.RestActionInvocation] (default task-16) Exception processing the result.: java.io.IOException: UT010029: Stream is closed
at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:136) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:128) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
at org.apache.struts2.rest.DefaultContentTypeHandlerManager.handleResult(DefaultContentTypeHandlerManager.java:199) [struts2-rest-plugin-2.3.34.jar:2.3.34]
at org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:232) [struts2-rest-plugin-2.3.34.jar:2.3.34]
at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:197) [struts2-rest-plugin-2.3.34.jar:2.3.34]
at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:145) [struts2-rest-plugin-2.3.34.jar:2.3.34]
at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:147) [xwork-core-2.3.34.jar:2.3.34]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:575) [struts2-core-2.3.34.jar:2.3.34]
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81) [struts2-core-2.3.34.jar:2.3.34]
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) [struts2-core-2.3.34.jar:2.3.34]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]