码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-06-07 13:15:51      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   java   a   ext   

最近项目需要用到上传下载,以前学jsp的时候直接用的是smartUpload,现在学的框架但是老师只是简单地教了框架的内容

对struts文件上传和下载没有涉及,没办法只能自己自学了!结果出现了上面的问题。

这个问题的根本原因网上都有说出来,但是没有给出的解决方案。原因是要返回的流为空,文件的路径有误导致文件的输入流为空!

所以最好在逻辑处理那块输出的你要下载文件的路径看是不是你要下载的路径!好了废话不多说哈!


package com.iss.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoad extends ActionSupport {


private String fileName;


private String directory;



public void setDirectory(String directory) {
this.directory = directory;
}


public String getFileName() {
return fileName;
}


public void setFileName(String fileName) {
this.fileName = fileName;
}


public InputStream getInputStream() throws FileNotFoundException {


String dir = directory + fileName;

System.out.println(dir);//输出的文件要下载的路径


// return ServletActionContext.getServletContext().getResourceAsStream(
// "/" +dir);//使用相对路径下载文件内容默认是和webRoot同一目录
return new FileInputStream(dir);//使用全局路径下载

}

@Override
public String execute() throws Exception {
System.out.println("fileName:" + fileName);
return SUCCESS;
}
}


struts.xml中的配置

<!--下载action -->
<action name="downloadAction" class="com.iss.action.DownLoad">
<!-- <param name="directory">/images/</param> -->下载路径为相对路径
<param name="directory">E:/wang/head/</param>下载路径为绝对路径
<result type="stream">
<!--指定下载文件内容的类型 -->
<param name="contentType">jpg/gif</param>
<!--inputName默认值inputStream -->
<param name="inputName">inputStream</param>
<!--动态文件获取 -->
<param name="contentDisposition">attachement;fileName="${fileName}"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">50000000</param>
</result>
<result name="input">error.jsp</result>
</action>

总结:无论使用相对路径还是绝对路径作为下载路径都要把要下载的文件路径写对否则就会出现上面的错误!


Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <,布布扣,bubuko.com

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

标签:c   style   class   java   a   ext   

原文地址:http://blog.csdn.net/wangdianyong/article/details/28596123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!