当我点击命令按钮时,UploadedFile为空。怎么了?在form标签中插入以下代码:enctype="multipart/form-data"
<h:form enctype="multipart/form-data">
<p:fileUpload mode="simple" value="#{b_cargar_tbl.file}" />
<p:commandButton actionListener="#{b_cargar_tbl.upload()}" value="Send" ajax="false" />
</h:form>
bean的代码为:
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
web.xlm
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto|native|commons</param-value>
</context-param>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
这个解决方案:将属性fileUploadListener
添加到p:fileUpload
中,并在带有一个参数侦听器的managedbean类方法中创建FileUploadEvent
的例子:
<p:commandButton actionListener="#{b_cargar_tbl.upload()}" value="Send"
fileUploadListener="#{b_cargar_tbl.upload}" ajax="false" />
在您的托管bean添加方法中:
public void upload(FileUploadEvent event) {
System.err.println("event.getFile().getFileName() = " + event.getFile().getFileName());
try {
copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
} catch (Exception ex) {
ex.printStackTrace();
}
}