练习struts2 文件下载,结果报错:
javax.servlet.ServletException: 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.
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)action如下:
 
package com.wjz.struts;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;public class FileDownloadAction implements Action {
    private String inputPath;
    
public String getInputPath() {
return inputPath;
} public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
    private InputStream getTargetFile() throws Exception
    {
     return ServletActionContext.getServletContext().getResourceAsStream(getInputPath());
    }
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}}
struts.xml 配置如下:      <default-action-ref name="download"/>
      <action name="download" class="com.wjz.struts.FileDownloadAction" >             
          <param name="inputPath">\upload\轨道交通图.jpg</param>
          <result name="success" type="stream">
              <param name="contentType">image/pjpeg</param>
              <param name="inputName">targetFile</param>
              <param name="contentDisposition">attachment;filename="struts.jpg"</param>
              <param name="bufferSize">4096</param>
          </result> 
       </action>
jsp代码,直接去调用下载的那个action:
 <s:a href="/Struts2Demo/download.action">文件下载</s:a>
请教各位大侠,哪里的原因?
先谢谢各位啦~~

解决方案 »

  1.   

    问题就在 return ServletActionContext.getServletContext().getResourceAsStream(getInputPath());getInputPath():要指定下载路径和文件名。例:/downLoad/a.gif
      

  2.   

    这个设置了: <param name="inputPath">\upload\轨道交通图.jpg</param>
      

  3.   

    换成,尝试一下:
    <param name="inputPath">/upload/轨道交通图.jpg</param>
      

  4.   

    在web.xml<filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  5.   

    web.xml如下<?xml version="1.0" encoding="UTF-8"?>  
    <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
       
    </filter>
    <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
        <filter>
       <filter-name>struts-cleanup</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>struts-cleanup</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
      <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
     
    </web-app>
      

  6.   

    private InputStream getTargetFile() throws Exception
    --》public
      

  7.   


    应该就是public 写成private的问题重新发布一下  确保类被重新编译过
      

  8.   

    异常还是Can not find a java.io.InputStream with the name [targetFile] in the invocation stack. Check the <param name="inputName"> 这个吗
      

  9.   

    如果还是这个异常 对一下你的 返回InputStream的方法是不是配置文件中的inputName配的名字的getter方法方法修饰符是public  返回值是InputStream如果用tomcat 删应用和 work重新编译发布
      

  10.   

    inputName是 targetFile
    方法为public InputStream getTargetFile() throws Exception
        {
         return ServletActionContext.getServletContext().getResourceAsStream(getInputPath());
        }在tomcat下删了应用和work目录,重新试了一遍,还是报原来那个错。
    郁闷中........
    我看到很多资料上都是这么做的阿,我写的咋就不成功呢。。
      

  11.   

    按照我的做法再不好用就没有办法了:1>.<param name="inputPath">/upload/轨道交通图.jpg</param>2>.FileDownloadAction extends ActionSupport
      

  12.   

            //文件路径
    private String inputPath;
    //资源文件流
    private InputStream inputStream;


    public String execute() throws Exception {

    inputStream = ServletActionContext.getServletContext().getResourceAsStream(inputPath);

    return SUCCESS;
    }

    public void setInputPath(String inputPath) {
    this.inputPath = inputPath;
    } public InputStream getInputStream() {
    return this.inputStream;
    }
      

  13.   

    <action name="action名" class="">
         <param name="inputPath">路径</param>
         <result name="success" type="stream">
         <param name="contentType">application/zip</param>
         <param name="contentDisposition">attachment;filename="xx.zip"</param>
         <param name="inputName">inputStream</param>
         <param name="bufferSize">2048</param>
         </result>
         </action>下载文件类型改成你需要的就行了