Primefaces fileupload仅适用于要上载的第一个文件

时间:2012-08-22 10:13:03

标签: spring jsf file-upload primefaces web.xml

我正在使用Primefaces,我遇到了fileuploader的问题。问题是它对我上传的第一个文件(调用fileUploadListener并且java方法上传文件)工作正常,但是如果我尝试上传另一个文件,则不再调用该监听器,并且永远不会触发java方法。如果我刷新页面,我可以上传另一个文件,但如果我尝试上传第二个文件,则会发生同样的事情。

xhtml文件中的代码:

<p:tab id="eConsentTabPanel" title="Informed Consent" disabled="#{patientHandlerAction.patientTabsStatus}">
  <h:form id="eConsentForm" enctype="multipart/form-data">
     <h:panelGrid id="eConsentPanelGrid">
        <p:fileUpload fileUploadListener="#{patientHandlerAction.handleFileUpload}" 
                      mode="advanced"
                      update="messages"
                      multiple="true"
                      auto="true"
                      sizeLimit="20971520"
                      label="Select File"
                      allowTypes="/(\.|\/)(gif|jpe?g|png|pdf|doc?x)$/"/>
        <p:growl id="messages" showDetail="true"/>  
     </h:panelGrid>
  </h:form>  
</p:tab>

java bean文件中的代码:

public void handleFileUpload(FileUploadEvent event) {

   ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();
   String filename = FilenameUtils.getName(event.getFile().getFileName());
   File result = new File(extContext.getRealPath("//uploaded//" + filename));

   try {
      FileOutputStream fileOutputStream = new FileOutputStream(result);

      byte[] buffer = new byte[6124];

      int bulk;
      InputStream inputStream = event.getFile().getInputstream();
      while (true) {
         bulk = inputStream.read(buffer);
         if (bulk < 0) {
            break;
         }
         fileOutputStream.write(buffer, 0, bulk);
         fileOutputStream.flush();
      }

      fileOutputStream.close();
      inputStream.close();

      messageTitle = "Message";
      messageBody = "The file" + event.getFile().getFileName() + " was succesfully uploaded!";

      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, messageTitle, messageBody));

   }

   catch (IOException e) {
      e.printStackTrace();

      messageTitle = "Message";
      messageBody = "The file was not uploaded";

      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, messageTitle, messageBody));
      FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The files were not uploaded!", "");
      FacesContext.getCurrentInstance().addMessage(null, error);
   }

}

web.xml文件中的代码:

<filter>
   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <filter-class>
      org.primefaces.webapp.filter.FileUploadFilter
   </filter-class>
   <init-param>
      <param-name>uploadDirectory</param-name>
      <param-value>/HerEhrUploaded/temp</param-value>
   </init-param>
   <init-param>
      <param-name>thresholdSize</param-name>
      <param-value>20971520</param-value>
   </init-param>      
</filter>

<filter-mapping>
   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

3 个答案:

答案 0 :(得分:3)

我补充说:

event.getComponent().setTransient(false);

内部文件上传监听器方法

public void handleUpload(FileUploadEvent event) {
    ....
}

它运作正常。

答案 1 :(得分:1)

我遇到了同样的问题,我找到了一个解决方案(解决方法比解决方案更多):

http://code.google.com/p/primefaces/issues/detail?id=6157

FileUpload 组件中的 isTransient()方法似乎存在问题。 希望它有所帮助。

答案 2 :(得分:0)

我想我发现了什么是错的,但我不确定导致这个问题的原因。 我用来显示fileUpload组件的xhtml页面有很多选项卡,每个选项卡都有自己的h:form。我创建了一个只有一个表单的新页面,fileUpload组件似乎在那里工作正常。