其他分享
首页 > 其他分享> > 春季-Primefaces文件上传仅适用于要上传的第一个文件

春季-Primefaces文件上传仅适用于要上传的第一个文件

作者:互联网

我正在使用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>

解决方法:

我补充说:

event.getComponent().setTransient(false);

内部文件上传监听器方法,

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

很好

标签:file-upload,jsf,primefaces,web-xml,spring
来源: https://codeday.me/bug/20191031/1979839.html