struts2文件下載出現Can not find a java.io.InputStream with the name的錯誤
註意:我遇到的問題原因是:不但成員變量中要有 imageStream 變量,同時 還要 有 對應的 get set 方法
具體可以參考下面:
struts2文件下載出現Can not find a java.io.InputStream with the name的錯誤
2011年08月16日 08:39:31 曹勝歡 閱讀數:65455 個人分類: 框架Struts2今天在用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;
}
struts2文件下載出現Can not find a java.io.InputStream with the name的錯誤