单击commandButton时,UploadedFile为null。什么错了? 在表单标签中我插入代码: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>
答案 0 :(得分:0)
此解决方案:
将属性fileUploadListener
添加到p:fileUpload
并使用一个参数监听器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();
}
}