JSF使用Primefaces上傳檔案無法動作解決方法
在WEB.XML中加入以下內容即可
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>2097152</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
XHTML內容
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{inPackingListCtrl.uploadFile}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
java內容
public void uploadFile(FileUploadEvent event) {
System.out.println("upload File start!!! ");
byte[] file = event.getFile().getContents();
System.out.println("upload fileName="+event.getFile().getFileName());
System.out.println("upload fileLength="+file.length);
System.out.println("upload file End !!! ");
}
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>2097152</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
XHTML內容
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{inPackingListCtrl.uploadFile}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
java內容
public void uploadFile(FileUploadEvent event) {
System.out.println("upload File start!!! ");
byte[] file = event.getFile().getContents();
System.out.println("upload fileName="+event.getFile().getFileName());
System.out.println("upload fileLength="+file.length);
System.out.println("upload file End !!! ");
}
留言
張貼留言