标签:struts2
1) Action类
package com.morris.ajax.action;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;
public class FileDownAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String fileName;
private InputStream inputStream;
public String getFileName() { return fileName; }
public void setFileName(String fileName) throws UnsupportedEncodingException { // 解决请求文件名中文乱码 fileName = new String(fileName.getBytes("iso-8859-1"), "utf-8"); this.fileName = fileName; } //对应配置文件中 <param name="inputName">inputStream</param>的getter方法 public InputStream getInputStream() throws FileNotFoundException, UnsupportedEncodingException {
String realPath = ServletActionContext.getRequest().getRealPath("/");
System.out.println(realPath + "file\\" + fileName);
InputStream inputStream = new FileInputStream(realPath + "file\\" + fileName); // 防止下载时候中文乱码 fileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
return inputStream; }
public String down() { return SUCCESS; } } |
2) struts2配置文件的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="fileDown" class="com.morris.ajax.action.FileDownAction" method="down"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="inputName">inputStream</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts> |
1) 在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容如下:
严重: Servlet.service() for servlet default threw exception java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:770) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619) |
2) 异常分析
stream对应的类是org.apache.struts2.dispatcher.StreamResult,该类的处理过程如下:
i. 配置其中result标签下的各个参数
ii. 从服务器中获取输入流,并同时与客户端建立输出流(服务器与客户端链接通过Socket进行连接)
iii. 当点击“保存”或“打开”时,开始传输数据。如果点击“取消”,关闭所有的流。
这里要注意的是,但是实际发现Socket并没有断开!并且流也没有关闭!这一点非常重要!
所以在JSP容器通过Response获取输出流之前,前面的流并没有关闭,所以会造成该异常的报出。
3) 异常解决
i. 将jar包struts2-sunspoter-stream-1.0.jar引入工程中
ii. 在原有的struts.xml的基础上进行相应的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <result-types> <result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX" /> </result-types> <action name="fileDown" class="com.morris.ajax.action.FileDownAction" method="down"> <result name="success" type="streamx"> <param name="contentType">text/plain</param> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="inputName">inputStream</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts> |
在这种方式下,只需添加一个result-type,将原有的result中type改为“streamx”,其他一律不变,在这种情况下,点击“取消”的同时也关闭了流,不会再报出该异常。
之后的执行“取消”后的结果如下
警告:StreamResultX Warn : socket write error
标签:struts2
原文地址:http://blog.csdn.net/u022812849/article/details/41116801