试用struts2的异常处理机制
配置文件 struts.xml<global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>....
<action ...>
<exception-mapping result="fileNotFound" exception="java.io.FileNotFoundException"></exception-mapping>
<result name="fileNotFound">/fileNotFound.jsp</result>
</action>
action中代码为://下载其他数据文件
public InputStream getData() throws Exception{
InputStream inStream = null;
HttpServletRequest request = ServletActionContext.getRequest();
String fdataid = request.getParameter("fdataid");
String type = request.getParameter("type");
// System.out.println("g"+type+"2");
String dataname = request.getParameter("fdataname");
// request.setCharacterEncoding("UTF-8");
//此处故意重新编译,获得乱码的dataname
dataname=new String(dataname.getBytes("ISO-8859-1"),"UTF-8");// System.out.println(dataname+"!!!!!!!!!!!!!!!!!!!2");
if(fdataid!=null){
try{
this.tbhead= munuservice.getHeadTableByDataid(Integer.valueOf(fdataid));
String location=this.tbhead.getFLocation();
GetProperties gp=new GetProperties();
//读取文件根路径的位置
String fileRoot=gp.getValue("StoreNextConfig.properties", "FileRoot");
String file=fileRoot+location+"\\"+dataname;
inStream=new FileInputStream(file);
dataname=dataname.replace("\\", "/");
//显示的文件名
String fileData[]=dataname.split("/");
if(fileData.length>0)
{
this.fileName= fileData[fileData.length-1];
}else
{
this.fileName=dataname;
}
}catch(FileNotFoundException fnex){
fnex.printStackTrace();
System.out.println("系统找不到文件:"+dataname);
throw new FileNotFoundException("系统找不到文件:"+dataname);
}
}else{
throw new FileNotFoundException("ID:"+fdataid+"对应的文件不存在!");
}
return inStream;
}系统运行后能够输出,后台输出为:
java.io.FileNotFoundException: Z:\corestore\store3\G\G01\G0101\G010101\G010101003\水体指数\???-WI.tfw (文件名、目录名或卷标语法不正确。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
...
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
系统找不到文件:???-WI.tfw
2010-07-07 10:47:58,968 [org.apache.struts2.dispatcher.StreamResult]-[ERROR] Can not find a java.io.InputStream with the name [data] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
但是在客户端跳转的异常处理界面是error.jsp
在error.jsp中输出exception.message,获得总是Can not find a java.io.InputStream with the name [data] in the invocation stack. Check the tag specified for this action.
谁能给解释一下,分数不够可以再加!
配置文件 struts.xml<global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>....
<action ...>
<exception-mapping result="fileNotFound" exception="java.io.FileNotFoundException"></exception-mapping>
<result name="fileNotFound">/fileNotFound.jsp</result>
</action>
action中代码为://下载其他数据文件
public InputStream getData() throws Exception{
InputStream inStream = null;
HttpServletRequest request = ServletActionContext.getRequest();
String fdataid = request.getParameter("fdataid");
String type = request.getParameter("type");
// System.out.println("g"+type+"2");
String dataname = request.getParameter("fdataname");
// request.setCharacterEncoding("UTF-8");
//此处故意重新编译,获得乱码的dataname
dataname=new String(dataname.getBytes("ISO-8859-1"),"UTF-8");// System.out.println(dataname+"!!!!!!!!!!!!!!!!!!!2");
if(fdataid!=null){
try{
this.tbhead= munuservice.getHeadTableByDataid(Integer.valueOf(fdataid));
String location=this.tbhead.getFLocation();
GetProperties gp=new GetProperties();
//读取文件根路径的位置
String fileRoot=gp.getValue("StoreNextConfig.properties", "FileRoot");
String file=fileRoot+location+"\\"+dataname;
inStream=new FileInputStream(file);
dataname=dataname.replace("\\", "/");
//显示的文件名
String fileData[]=dataname.split("/");
if(fileData.length>0)
{
this.fileName= fileData[fileData.length-1];
}else
{
this.fileName=dataname;
}
}catch(FileNotFoundException fnex){
fnex.printStackTrace();
System.out.println("系统找不到文件:"+dataname);
throw new FileNotFoundException("系统找不到文件:"+dataname);
}
}else{
throw new FileNotFoundException("ID:"+fdataid+"对应的文件不存在!");
}
return inStream;
}系统运行后能够输出,后台输出为:
java.io.FileNotFoundException: Z:\corestore\store3\G\G01\G0101\G010101\G010101003\水体指数\???-WI.tfw (文件名、目录名或卷标语法不正确。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
...
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
系统找不到文件:???-WI.tfw
2010-07-07 10:47:58,968 [org.apache.struts2.dispatcher.StreamResult]-[ERROR] Can not find a java.io.InputStream with the name [data] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
但是在客户端跳转的异常处理界面是error.jsp
在error.jsp中输出exception.message,获得总是Can not find a java.io.InputStream with the name [data] in the invocation stack. Check the tag specified for this action.
谁能给解释一下,分数不够可以再加!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货