struts2文件下载出现Can not find a java.io.InputStream with the name的错误

3 篇文章 0 订阅
ava.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the tag specified for this action. 
    org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237) 
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186) 
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266) 
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252) 
---------------------------------------------------------------------------------- 
解决方法:打印InputStream,如果为null,则问题找到了。fileName为空值导致InputStream中返回的ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileName)为null,报出以上异常,找到了异常的根源。
将文件路径打出来,发现fileName为空,即struts获取对象属性失败,然后对此问题进行相关处理,并解决。
==========================================================

strut2下载文件的“ <param name="inputName"> tag specified for this action.”异常  

具体异常是这句话:

Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.


1.文件路径不对,根本就没有取到文件。这种情况下,可以将获得InputStream的那条语句放在system.out.println()中输出一下,若为null,那就是路径不对了,或者说得准确些就根本没有找到文件。
2.在action中没有写配置文件中"<param name="inputName">"后面属性的那个get方法.

页面上:
<a href="fileDownload.action?fileName=<s:property value ="imageName" />">下载此图片</a>
struts.xml中
----------------------------------------------------------
<!-- 文件下载,支持中文附件名 -->
   <action name="fileDownload"
    class="com.test.action.filedown.FileDownloadAction">
    <result name="success" type="stream">
     <!-- 动态文件下载的,事先并不知道未来的文件类型,那么我们可以把它的值设置成为:application/octet-stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错; -->
     <param name="contentType">
     application/octet-stream;charset=ISO8859-1
     </param>
     <param name="contentDisposition">
      attachment;filename="${downloadFileName}"
     </param>
     <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
      对应action类中的方法 getDownloadFileName() 其中特殊的代码就是${downloadFileName},它的效果相当于运行的时候将action对象的属性的取值动态的填充在${}中间的部分,我们可以认为它等价于+action. getDownloadFileName()。 -->
     <param name="inputName">inputStream</param>
     <param name="bufferSize">4096</param>
    </result>
   </action>
----------------------------------------------------------
action中
----------------------------------------------------------
private String fileName;// 初始的通过param指定的文件名属性 set get

/** 文件名 转换编码 防止中文乱码*/
public String getDownloadFileName() {
   String fileName=ServletActionContext.getRequest().getParameter("fileName");
   String downFileName = fileName;
   try {
    downFileName = new String(downFileName.getBytes(), "ISO8859-1");
   } catch (Exception e) {
    e.printStackTrace();
   }
   return downFileName;
}
//下载的流
public InputStream getInputStream() {
   String name=this.getDownloadFileName();
//  String realPath=ServletActionContext.getServletContext().getRealPath("/uploadImages")+ "/"+name; 路径错误
   String realPath="/uploadImages/"+name;
   InputStream in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
   if(null==in){
    System.out.println("Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name=\"inputName\"> tag specified for this action.检查action中文件下载路径是否正确.");   
   }
   return ServletActionContext.getServletContext().getResourceAsStream(realPath);
}

@Override
public String execute() throws Exception {
   return SUCCESS;
}

当以上两种情况都正确的情况下,问题就在这里了:
      当采用 return ServletActionContext.getServletContext().getResourceAsStream("...") 这种方法获得输入流的时候,要保证文件位置在 ServletContext 当中,就是说要在当前的应用上下文中,
如果想要获得外部文件 譬如 D盘中的某个文件,那么就要自己创建输入流才可以,如:
File  file  new  File("D:\\spring.doc");     
     
InputStream  is  new  FileInputStream(file);     
     
return  is;     
File file = new File("D:\\spring.doc"); InputStream is = new FileInputStream(file); return is; 
则OK!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值