今天在用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;
}

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

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

    成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontN ...

  2. 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 严重: Excepti ...

  3. java.io.StreamCorruptedException: invalid type code: AC错误的解决方法

    问题描述: 在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)间接的构建了ObjectOutputStream流 ...

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

    package 文件操作; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impor ...

  5. 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 ...

  6. 关于Java IO InputStream 的一点整理!

    程序的开发其中一直在用文件的读写.可是对于java其中输入流以及输出流仅仅是会用不理解,一直以来想搞清楚其,可是一直没有运行(悲剧).今天早上抽出半个小时通过JDK API1.6.0中文版帮助逐步的了 ...

  7. java io InputStream 转 byte

    InputStream is ; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024] ...

  8. Struts2文件下载

    1). Struts2 中使用 type="stream" 的 result 进行下载 2). 可以为 stream 的 result 设定如下参数 contentType: 结果 ...

  9. Struts2 文件下载

    使用Struts2做一个简单的文件下载. 首先,导包,写配置文件就不说了. 进入主题. 文件下载操作类:FileDownload.java import java.io.InputStream; im ...

随机推荐

  1. 1.shell之搭建Shell编程环境

    第一次写博客,加点废话,学习linux有一段时间,随着学习的深入发现自己学的不够系统,特别是遇到一些莫名的问题时,我只有各种百度,运气好时能解决掉,差时到现在还没解决,就算解决了还是不清楚是怎么解决的 ...

  2. Asp.Net MVC是否针对每次请求都重新创建一个控制器实例

    一.Asp.Net MVC是否针对每次请求都重新创建一个控制器实例 默认情况下,答案是确定的. ControllerBuilder类 ControllerBuilder.Current用户获取默认的控 ...

  3. SqlServer2008误操作数据(delete或者update)后恢复数据

    实际工作中,有时会直接在数据库中操作数据,比如对数据进行delete或者update操作,当进行这些操作的时候,如果没有加上where条件或者where条件不合理,那么导致的结果可想而知,如果操作的又 ...

  4. 学习笔记_Java_day14—编码实战___一个注册页面的完整流程

  5. Oracle在表上建立自增字段的方法

    本方法为使用SEQUENCE(序列)   例如有表temp_test结构为: create table TEMP_TEST ( id number, nm varchar(10), primary k ...

  6. (转)iOS中3种正则表达式的使用与比较

    .利用NSPredicate(谓词)匹配 例如匹配有效邮箱: NSString *email = @“nijino_saki@.com”: NSString *regex = @"[A-Z0 ...

  7. 【ADO.NET】5、手机归属地查询( winfrom )

    using System.IO; 有一个数据库手机号码的txt文件,格式是 : 13500000000-13560000000-中国移动 查询结果: 湖南移动[邵阳]文件夹选择对话框 FolderBr ...

  8. centos 6.5 安装jenkins

    Installation sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...

  9. ASP.NET MVC5 easyui 之 treegrid 初用记录

    菜鸟初次使用,参考论坛中介绍的方法仍走了一些弯路,把自己遇到的问题记录下来. 1.必须定义根节点: 2.根节点一个或多个均可: 4.根节点的父节点属性不必定义,或者定义为0: 5.各级子节点的父节点属 ...

  10. 网站开发常用jQuery插件总结(十)菜单插件superfish

    网站对于菜单的依赖其实并不是很大,我们完全可以不使用菜单来设计网站,显示网站内容.但是如果网站的分类太多,“也许”使用菜单作为网站导航可以使 用户 更方便的寻找内容.superfish插件就是用于实现 ...