2013-05-17 51 views
0

我想上傳與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) 

回答

1

這裏供大家參考。

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中 - 確保配置

<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

好的,謝謝@CycDemo – joice

+0

你有一個很大的吻我,它搜索4小時後工作+1 –

0

嘗試下面的代碼:

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() { 
} 
} 

其中ent是實體類的對象,img(setImg)是BLOB類型的數據庫列。您只需在您的項目下的xHTML文件所在的web文件夾下創建「temp-images」文件夾。告訴我你是否仍然有任何問題。

+0

好非常感謝,我會嘗試這個代碼 – joice

+0

我已經試過了你的代碼,它的工作非常感謝@Jitesh。 – joice

+0

我有另一個問題,當我用fileUpload在同一個表單中添加兩個輸入文本字段,它不再保存圖像在數據庫中,我沒有顯示錯誤或異常。請問我該如何解決這個問題? – joice