1. 程式人生 > >(檔案下載)Can not find a java.io.InputStream with the name [inputStream] in the invocation stack

(檔案下載)Can not find a java.io.InputStream with the name [inputStream] in the invocation stack

2010-1-22 9:45:03 org.apache.struts2.dispatcher.StreamResult doExecute
嚴重: 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.
2010-1-22 9:45:03 org.apache.catalina.core.StandardWrapperValve invoke
嚴重: Servlet.service() for servlet default threw exception
java.lang.IllegalArgumentException: 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.
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.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)

==========================解決辦法===========================
這個錯誤報的真有點誤導人呀,我還一直以為我struts.xml中<param name="inputName">inputStream</param>和action中的方法名getInputStream()不一致的問題,大家如果也碰到此類問題,直接列印
InputStream in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
System.out.println(in);

如果列印為NULL的話,恭喜您,問題得以解決,問題的原因是這個流的realPath路徑錯誤,像我的,
//  String realPath=ServletActionContext.getServletContext().getRealPath("/uploadImages")+ "/"+name; 路徑錯誤


   String realPath="/uploadImages/"+name;正確的
=====================================================
沒明白的,請看下面的詳細說來
怪呀,我的配置應該沒錯呀
頁面上:
<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;
}