我想上傳與fileuploader但它返回null primefaces文件,primefaces FileUploadEvent返回null
addPhotos.xhtml:
<h:form id="importDevicesForm" enctype="multipart/form-data">
<h:outputText value="Photo :" />
<p:fileUpload id="scriptUpload"
widgetVar="importDevicesWidget"
fileUploadListener="#{docBean.file}"
auto="true"
label="Choisir une photo.."
mode="advanced"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/">
<h:outputText value="Description :" />
<p:commandButton value="Ajouter" action="#{docBean.ajouter_photo}"/>
</h:form>
我支持bean:我要上傳的文件中使用一個將文件寫入文件系統的outputStream。
@ManagedBean(name = "docBean")
@SessionScoped
public class DocumentBean implements Serializable {
private static final long serialVersionUID = 1L;
private UploadedFile file = null;
private File doc;
private InfoDAO docdao = new InfoDaoImpl();
public UploadedFile getFile() {
return file;
}
public void setFile(FileUploadEvent event) {
this.file = event.getFile();
}
public String ajouter_photo() throws SQLException, IOException
{
System.out.println("call");
File targetFolder = new File("C:/images/upload");
InputStream inputStream = this.file.getInputstream();
OutputStream out = new FileOutputStream(new File(targetFolder,
this.file.getFileName()));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
Document f = new Document();
f.setDescription(targetFolder.getPath());
docdao.Ajouter_info(f);
}
這裏是例外
Avertissement: #{docBean.ajouter_photo}: java.lang.NullPointerException
javax.faces.FacesException: #{docBean.ajouter_photo}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
好的,謝謝@CycDemo – joice
你有一個很大的吻我,它搜索4小時後工作+1 –