练习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>
请教各位大侠,哪里的原因?
先谢谢各位啦~~
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>
请教各位大侠,哪里的原因?
先谢谢各位啦~~
<param name="inputPath">/upload/轨道交通图.jpg</param>
<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>
<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>
--》public
应该就是public 写成private的问题重新发布一下 确保类被重新编译过
方法为public InputStream getTargetFile() throws Exception
{
return ServletActionContext.getServletContext().getResourceAsStream(getInputPath());
}在tomcat下删了应用和work目录,重新试了一遍,还是报原来那个错。
郁闷中........
我看到很多资料上都是这么做的阿,我写的咋就不成功呢。。
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;
}
<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>下载文件类型改成你需要的就行了