gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:51:38 28 4
gpt4 key购买 nike

我正在使用 Primefaces,但文件 uploader 有问题。问题是它对我上传的第一个文件工作正常(调用 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) {
....
}

它工作正常。

关于spring - Primefaces 文件上传仅适用于要上传的第一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12070901/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com