您的位置:首页 > 编程语言 > Java开发

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

2014-01-02 11:30 706 查看
今天在用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;

}

具体异常是这句话:

Can not find a java.io.InputStream with the name [downloadFile] 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方法.

当以上两种情况都正确的情况下,问题就在这里了:

当采用 return ServletActionContext.getServletContext().getResourceAsStream("...") 这种方法获得输入流的时候,要保证文件位置在
ServletContext 当中,就是说要在当前的应用上下文中,

如果想要获得外部文件 譬如 D盘中的某个文件,那么就要自己创建输入流才可以,如:

[java] view
plaincopy

File file = new File("D://spring.doc");

InputStream is = new FileInputStream(file);

return is;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐