当前位置: 代码迷 >> Web前端 >> struts2文件上载发生java.lang.IllegalStateException
  详细解决方案

struts2文件上载发生java.lang.IllegalStateException

热度:846   发布时间:2012-11-05 09:35:12.0
struts2文件下载发生java.lang.IllegalStateException

?先说说程序设计的背景,功能就是实现文件下载,思路是先生成一个临时文件,下载后要删除这个临时文件。再这里我用struts2。先想到是直接用普通的配置方式处理问题,也就是不用显示的获得httpServletResponse对象,然后对httpServletResponse进行操作的方式,配置大致如下:

写道
<action name="downLoadAccountCheck" class="downLoadAccountCheckAction">
<result name="errorpage">./downerror.jsp</result>
<result name="success" type="stream">
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<param name="inputName">inputStream</param>
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性对应action类中的方法 getDownloadFileName() -->
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>

?但是这样的方法有一个问题,那就是文件无法删除。那就只好用传统的方法了,也就是上述的先获得httpServletResponse对象,然后对其操作的方法,代码如下

写道
public String spInfo() {
System.out.println("到达了spInfo1111111111"+spName);
try {
// 获取当前登录帐号
String operatorId =ActionUtil.getLoginUserID();
System.out.println("operatorId="+operatorId);
//权限当前取和按sp查询交易记录报表一致
int right = AuthorityUtil.getTopRight(operatorId, "ID_RETRANS_STATBYSP_REPORT");

String district=null;
if(right == AuthorityUtil.LOCAL_DATA_VISIBLE){
String local=ActionUtil.getLocalID(operatorId);
if(local==null||local.equals("")){
this.message = "查询成功!";
success = true;
return SUCCESS;
}

//如果登录用户不是省公司用户或维护人员,需要进行地域限制
if(!local.equals("ALL")&&!local.equals("DW")){
district = local;
}

}
if(right == AuthorityUtil.INVISIBLE){
this.message = "查询成功!";
success = true;
return SUCCESS;
}

list = reTxnLogDAO.queryTradeSp(spName, spState,district, startDate, endDate);
File file=new File(getFileNameForSPinfo(spName, spState, startDate, endDate)+".xls");
//File file=new File("d:/sptemp.xls");
if(file.exists()){
file.delete();
}
file.createNewFile();
System.out.println("file----abs:"+file.getAbsolutePath());
createSPInfoExcel(file, list);
downloadExcel(file, ServletActionContext.getResponse());
delExcel(file);
//message = "查询成功!";
//success = true;
} catch (Exception e) {
message = "查询失败!";
success = false;
e.printStackTrace();
}
return SUCCESS;
}

?但是新的问题又出现,那就是程序抛出了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:742)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:513)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:422)
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(Unknown Source)

?虽然说出现了上述异常不妨碍程序的使用,但是看起来很别扭,很不爽,经过研究把上述代码中的return SUCCESS 改成return null 问题得到解决!

1 楼 熊笨笨 2010-12-08  
呵呵。。。谢谢,我也遇到了这个问题。。。现在终于解决了。
2 楼 我爱java 2012-08-13  
有的功能return success 以后再struts.xml 还要接收这个success 做下一步的处理,想问一下return null以后功能岂不失效了么?怎么解决的?
  相关解决方案