本文介绍了primefaces FileUploadEvent返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用primefaces fileuploader上传文件,但是它返回null,

I'm trying to upload a file with primefaces fileuploader but it returns null,

addPhotos.xhtml:

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在文件系统中写入文件.

My backing bean : I want to upload the file an use an outputStream to write the file in filesystem.

@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)

推荐答案

在这里,仅供参考.

upload.xhtml

upload.xhtml

    <h:form enctype="multipart/form-data">
        <p:fileUpload value="#{PrimefacesFileUpload.uploadedFile}" mode="simple" sizeLimit="50000"
                      allowTypes="/(\\\\\\\\./\\\\\\\\/)(gif|jpe?g|png|txt)$/"/>
        <p:growl id="messages" showDetail="true"/>
        <p:commandButton value="Submit" actionListener="#{PrimefacesFileUpload.upload}" ajax="false"/>
    </h:form>

PrimefacesFileUpload.java

PrimefacesFileUpload.java

@ManagedBean(name = "PrimefacesFileUpload")
public class PrimefacesFileUpload {
    private UploadedFile uploadedFile;

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void upload(ActionEvent event) {
        String fileName = uploadedFile.getFileName();
        byte[] content = uploadedFile.getContents();
        String contentType = uploadedFile.getContentType();
        // Keep upload file
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Successful! " + uploadedFile.getFileName() + " is uploaded."));
    }
}

web.xml-确保配置

web.xml - Make sure to config

<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>51200</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

这篇关于primefaces FileUploadEvent返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:54
查看更多