日期:2014-05-19  浏览次数:20784 次

简单文件下载中找不到输入流,向大家求助
大家帮忙看看这个问题怎么解决?非常感谢!

(I)这是Tomcat报的exception: 
2010-3-29 16:11:26 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Can not find a java.io.InputStream with the name [targetFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action.


(II)这是浏览器报的exception: 
Struts has detected an unhandled exception: 

Messages: Can not find a java.io.InputStream with the name [targetFile] in the invocation stack. Check the tag specified for this action. 
 
File: org/apache/struts2/dispatcher/StreamResult.java 
Line number: 237 


(III)这是我的struts.xml
<package name="download" extends="struts-default">
  <action name="download" class="org.struts2.Upload.FileDownloadAction">
  <param name="inputPath">\files\abc.jpg</param>
  <result name="success" type="stream">
  <param name="contentType">image/jpg</param>
  <param name="inputName">targetFile</param>
  <param name="contentDisposition">filename="abc.jpg"</param>
  <param name="bufferSize">4096</param>
  </result>
</action>
</package>


(IV)这是我的Action:
package org.struts2.Upload;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileDownloadAction extends ActionSupport{
private String inputPath;

public void setInputPath(String value) {
inputPath = value;
}

public InputStream getTargetFile() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
}


(V)HTML中的链接:
<h3>文件下载</h3>
  <ul>
  <li><a href="download.action">点此下载</a></li>
  </ul>

(VI)web.xml是没有问题的。

------解决方案--------------------
extends ActionSupport 这个类是你自己写的吗,struts中没有这个
------解决方案--------------------
Check the <param name="inputName"> tag specified for this action.

没这个inputName属性啊,提示很明显,叫在这个ACTION中声明一个inputName属性,并且需要SET,GET方法

我们点击下载的时候,ACTION把路径传给inputName属性的SET方法,下载的信息流会到GET方法里找到你在配置文件中给inputName属性指定的参数啊,你没inputName属性,你叫ACTION去哪找....
------解决方案--------------------
没用过 路过帮顶
------解决方案--------------------
看来你不会用Struts2啊
你在页面传过来的参数
需要Action的类中有对应匹配类型并且名字完全一致的参数与之对应的
并且需要set和get方法
如果你没有
你点击页面的那些东西
根本没用的

你的ActionSupport类看起来好奇怪啊
------解决方案--------------------
我刚刚也在弄这个,弄了半天,发现我把图片的位置放错了,因为开始在写上传的,写下载的时候就用了上传的文件,根本没注意这个文件夹没在当前工程中。呵呵。。。看了下你的好像没哪里有问题哦,你看看是不是文件位置没放好。出现这问题是ServletActionContext.getServletContext().getResourceAsStream(path);为null,一般就是路径问吧!!!呵呵,我也是初学者,看见是29号的就回下。。。。。。