我正在尝试使用 primefaces fileuploader 上传文件,但它返回 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)