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

今天在用struts2就行文件下载时出现如下错误:

    Servlet.service() for servlet default threw exception
    java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [imageStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
        at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:189)
        at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
        at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
        at com.best.top.validate.TopInterceptor.intercept(TopInterceptor.java:47)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
        at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
        at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
        at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
        at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)  

说实话这个提示真有误导人的嫌疑,刚开始还以为是名称不对,估计一般人看到这个提示都这样想。然后查看StreamResult的源代码才发现是因为InputStream为null的缘故,汗一个。看下源码:

if (inputStream == null) {
                    // Find the inputstream from the invocation variable stack
                    inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
                }   

                if (inputStream == null) {
                    String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +
                        "Check the <param name=\"inputName\"> tag specified for this action.");
                    LOG.error(msg);
                    throw new IllegalArgumentException(msg);
                }  

大家如果也碰到此类问题,直接打印

InputStream in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
System.out.println(in);

如果打印为NULL的话,恭喜您,问题得以解决,问题的原因是这个流的realPath路径错误,还没明白的往下看,怪呀,我的配置应该没错呀

页面上:

<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;
}
时间: 2025-01-01 06:37:34

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

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

成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontNewsAction_downloadFile.action?fileName=<s:property value="fileTitle"/>">下载</a> Action文件:private String fileName;//get set方法 p

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

1.错误描写叙述 八月 14, 2015 4:22:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Exception occurred during processing request: Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the <param name=

Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the &lt;

最近项目需要用到上传下载,以前学jsp的时候直接用的是smartUpload,现在学的框架但是老师只是简单地教了框架的内容 对struts文件上传和下载没有涉及,没办法只能自己自学了!结果出现了上面的问题. 这个问题的根本原因网上都有说出来,但是没有给出的解决方案.原因是要返回的流为空,文件的路径有误导致文件的输入流为空! 所以最好在逻辑处理那块输出的你要下载文件的路径看是不是你要下载的路径!好了废话不多说哈! package com.iss.action; import java.io.Fil

【java】io流之字节输入流:java.io.InputStream类及子类java.io.FileInputStream

1 package 文件操作; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 public class TestInputStream { 9 public static void main(String[] args) throws IOException { 10 File file=new F

Java IO: InputStream

原文链接 作者: Jakob Jenkov 译者: 李璟([email protected]) InputStream类是Java IO API中所有输入流的基类.InputStream子类包括FileInputStream,BufferedInputStream,PushbackInputStream等等.参考Java IO概述这一小节底部的表格,可以浏览完整的InputStream子类的列表. Java InputStream例子 InputStream用于读取基于字节的数据,一次读取一个字

关于Java IO InputStream 的一点整理!

程序的开发当中一直在用文件的读写,但是对于java当中输入流以及输出流只是会用不理解,一直以来想搞清楚其,但是一直没有执行(悲剧),今天早上抽出半个小时通过JDK API1.6.0中文版帮助逐步的了解下字节输入流读取字节的方法: 下面就说说InputStream当中read().read(byte[]  b).read(byte[] b.int off .int len)的使用以及区别 一.read()方法: public static void inputStreamRead1() { try

Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData()

项目运行的时候,如果报错 Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData() 解决方案: 在app的build.gradle文件中添加以下代码 apply plugin: 'com.android.applicat

JAVA IO InputStream InputStreamReader BufferedReader

InputStream.OutputStream 处理字节流的抽象类,InputStream能从來源处读取一個一個byte,所以是最低级的.InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等 InputStreamReader  OutputStreamWriter 处理字符流的抽象类,是字节流通向字符流的桥梁,它将字节流转换为字符流.它以较高级的方式,一次读取一个一个字符. BufferedReader BufferedWriter B

struts2 文件下载 报错

程序错误: 学习struts框架时,关于文件下载部分,利用struts中的stream结果类型来实现,配置完成之后,运行程序,报错如下: HTTP Status 500 - Can not find a java.io.InputStream with the name [downFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action. 错