3

我正在使用 Primefaces,但文件上传器有问题。问题是它适用于我上传的第一个文件(调用 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>
4

3 回答 3

3

我补充说:

event.getComponent().setTransient(false);

内部文件上传监听方法,

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

它工作正常。

于 2015-05-19T04:08:16.953 回答
1

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

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

FileUpload组件中的isTransient()方法似乎有问题。希望能帮助到你。

于 2014-02-24T13:42:36.660 回答
0

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

于 2012-08-24T09:18:06.293 回答