2014-01-13 59 views
4

我想從運行Struts2,Hibernate和Spring的web服務器上下載文件,但我總是得到這個異常。在struts 2中下載文件

java.lang.IllegalArgumentException: 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. 
    org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237) 
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186) 
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275) 
    org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) 
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.riteshsangwan.ossoc.core.interceptors.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:34) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) 
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562) 
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) 

我檢查了inputName參數並調試了項目,找出InputStream不爲null。

這是下載類

inputName= new ByteArrayInputStream(mf.getFileContent()); 

,這是Struts 2的映射

 <action name="downloadfile" class="com.riteshsangwan.ossoc.core.actions.DownloadFile"> 
      <result name="success" type="stream"> 

       <param name="inputName">inputName</param> 
       <param name="contentDisposition">attachment;filename=${fileName}</param> 
       <param name="contentType">${contentType}</param> 
       <param name="bufferSize">1024</param> 
       <param name="contentLength">${contentLength}</param> 
       <param name="allowCaching">false</param> 

      </result> 
      <result name="input">/user/files.jsp</result> 
     </action> 

是因爲ByteArrayInputStream的或任何其他原因的除外。我有所有變量的公共getter和setter方法,並且我檢查了它們都不是null。

EDITS:DownloadFile類

package com.riteshsangwan.ossoc.core.actions; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
import com.riteshsangwan.ossoc.core.dao.FilesBo; 
import com.riteshsangwan.ossoc.core.entities.Files; 
import com.riteshsangwan.ossoc.core.entities.Users; 
import com.riteshsangwan.ossoc.core.util.MyFile; 
import com.riteshsangwan.ossoc.core.util.Operations; 
import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.List; 
import java.util.Map; 
import javax.servlet.ServletContext; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.context.ServletContextAware; 


public class DownloadFile extends ActionSupport implements ServletContextAware{ 

    private long fid; 
    private String contentType; 
    private long contentLength; 
    private String fileName; 
    private InputStream inputName; 


    private ServletContext context; 
    private String downloadStatus; 
    private String downloadpassword; 
    private List fileList; 
    private Map<String,Object> session; 

    public String getFileName() { 
     return fileName; 
    } 

    public void setFileName(String fileName) { 
     this.fileName = fileName; 
    } 


    public List getFileList() { 
     return fileList; 
    } 

    public void setFileList(List fileList) { 
     this.fileList = fileList; 
    } 


    public String getDownloadStatus() { 
     return downloadStatus; 
    } 

    public void setDownloadStatus(String downloadStatus) { 
     this.downloadStatus = downloadStatus; 
    } 

    public String getDownloadpassword() { 
     return downloadpassword; 
    } 

    public void setDownloadpassword(String downloadpassword) { 
     this.downloadpassword = downloadpassword; 
    } 

    public String getStatus() { 
     return downloadStatus; 
    } 

    public void setStatus(String downloadStatus) { 
     this.downloadStatus = downloadStatus; 
    } 

    @Autowired 
    private FilesBo fbo; 

    @Autowired 
    private Operations operation; 

    public Operations getOperation() { 
     return operation; 
    } 

    public void setOperation(Operations operation) { 
     this.operation = operation; 
    } 

    public FilesBo getFbo() { 
     return fbo; 
    } 

    public void setFbo(FilesBo fbo) { 
     this.fbo = fbo; 
    } 


    public long getFid() { 
     return fid; 
    } 

    public void setFid(long fid) { 
     this.fid = fid; 
    } 

    public String getContentType() { 
     return contentType; 
    } 

    public void setContentType(String contentType) { 
     this.contentType = contentType; 
    } 

    public long getContentLength() { 
     return contentLength; 
    } 

    public void setContentLength(long contentLength) { 
     this.contentLength = contentLength; 
    } 


    public InputStream getInputName() { 
     return inputName; 
    } 

    public void setInputName(InputStream inputName) { 
     this.inputName = inputName; 
    } 


    @Override 
    public String execute(){ 
     String response; 
     int a=10; 
     try 
     { 
      session=ActionContext.getContext().getSession(); 
      Users u= (Users) session.get("user"); 
      long uid=u.getUid(); 
      fileList=fbo.getAllFiles(uid); 
      Files f=getFbo().getFileWithData(getFid()); 
      MyFile mf=new MyFile(); 
      mf.setFileContent(f.getFileData()); 
      mf.setFileContentType(f.getFileContentType()); 
      mf.setFileHash(f.getFileHash()); 
      mf.setFileName(f.getFileName()); 
      mf.setFileSize(f.getFileSize()); 
      mf.setPassword(downloadpassword); 
      mf=operation.decrypt(mf); 

      fileName=mf.getFileName(); 
      contentLength=mf.getFileSize(); 
      contentType=mf.getFileContentType(); 

      inputName=new ByteArrayInputStream(mf.getFileContent()); 

      response=ActionSupport.SUCCESS; 
     } 
     catch(IOException e) 
     { 
      if(e.getMessage().equalsIgnoreCase("Message has been altered or password incorrect")) 
      { 
       downloadStatus="Message has been altered or incorrect password. Try again."; 
      } 
      response=ActionSupport.INPUT; 
     } 
     catch(Exception e) 
     { 
      downloadStatus="Some error occured. Try Again"; 
      response=ActionSupport.INPUT; 
     } 
     return response; 
    } 

    @Override 
    public void setServletContext(ServletContext servletContext) { 
     this.context=servletContext; 
    } 

} 
+0

顯示你的'DownloadFile'行動。 –

+0

@AleksandrM請參閱EDITS – user123

+0

@ user3172529您的'inputName'參數不是'InputStream'類型。檢查上下文中的值。 –

回答

2

好吧,改變inputNameinputStream。像這樣

private InputStream inputStream; 

public InputStream getInputStream() { 
    return inputStream; 
} 

public void setInputStream(InputStream inputStream) { 
    this.inputStream= inputStream; 
} 

的參數inputName默認設置爲inputStream,那麼,你可能會刪除它。另請注意,結果中的參數名稱將在值棧中查找。因此,如果您在操作中公開了像getInputName這樣的獲取方式,則意味着您要覆蓋配置中的值。

+0

你可以看看 http://stackoverflow.com/questions/21076569/jsp-value-of - 變化後改變模型彈出 我不明白爲什麼價值改變後模式彈出即使他們在同一頁 – user123

0

確保getInputName()方法不返回null,如果它返回null,則會發生此異常。

這個異常的描述是誤導性的,它會讓你覺得你的inputName參數配置的名稱是錯誤的。但是,實際的原因是getInputName()的返回InputStream爲null。

0

  <param name="inputName">inputName</param> 
      <param name="contentDisposition">attachment;filename=${fileName}</param> 
      <param name="contentType">${contentType}</param> 
      <param name="bufferSize">1024</param> 
      <param name="contentLength">${contentLength}</param> 
      <param name="allowCaching">false</param> 


      *<!--add a iuputStream--> 
      <param name="inputStream">inputName</param>* 

     </result> 
     <result name="input">/user/files.jsp</result> 
    </action> 
+1

請解釋你的答案有點? – Ray