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