primefaces FileUploadEvent returns null

后端 未结 2 753
南笙
南笙 2021-01-27 14:51

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

addPhotos.xhtml :



        
相关标签:
2条回答
  • 2021-01-27 15:38

    Try following code:

    xhtml:

    <h:form enctype="multipart/form-data">
    
            <p:fileUpload 
                id="scriptUpload" 
                widgetVar="importDevicesWidget" 
                fileUploadListener="#{imageUpload_2.handleFileUpload}"  
                value="#{imageUpload_2.uploaded_image}"
                auto="true"
                label="Choisir une photo.."
                mode="advanced"  
                allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                />  
            <p:commandButton id="btn_save"
                             value="Save To Database"                             
                             actionListener="#{imageUpload_2.btn_save_clicked}"/>
        </h:form>
    

    jsf:

    @ManagedBean
    @ViewScoped
    public class ImageUpload_2 implements Serializable {
    
    UploadedFile uploaded_image;
    
    public UploadedFile getUploaded_image() {
        return uploaded_image;
    }
    
    public void setUploaded_image(UploadedFile uploaded_image) {
        this.uploaded_image = uploaded_image;
    }
    String upload_location;
    
    public String getUpload_location() {
        return upload_location;
    }
    
    public void setUpload_location(String upload_location) {
        this.upload_location = upload_location;
    }
    
    public void handleFileUpload(FileUploadEvent event) {
        uploaded_image = event.getFile();
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String v_file_ext = uploaded_image.getFileName().split("\\.")[(uploaded_image.getFileName().split("\\.").length) - 1];
        upload_location = servletContext.getRealPath("") + File.separator + "temp-images" + File.separator + "3" + "." + v_file_ext;
        FileImageOutputStream imageOutput;
        try {
            imageOutput = new FileImageOutputStream(new File(upload_location));
            imageOutput.write(uploaded_image.getContents(), 0, uploaded_image.getContents().length);
            imageOutput.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    
    public void btn_save_clicked(ActionEvent event) {
    
        byte[] file = new byte[uploaded_image.getContents().length];
        System.arraycopy(uploaded_image.getContents(), 0, file, 0, uploaded_image.getContents().length);
        //ent.setImg(file);
        //yourfacade.create(ent);
    }
    
    public ImageUpload_2() {
    }
    }
    

    Where ent is the object of Entity Class and img (setImg) is database column of BLOB type.You just need to create "temp-images" folder in you project under web folder where xhtml files are lying. Tell me if you are still having any issue.

    0 讨论(0)
  • 2021-01-27 15:53

    Here, it is for your reference.

    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

    @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 - 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>
    
    0 讨论(0)
提交回复
热议问题