2011-08-12 4 views
0

私はこれと同様の問題がありますFile Download Does Not Work。しかし、私はすべての答えを示そうとしたが役に立たなかった。私はまだNEPを手に入れます。誰かがNPE on filed in primefaces

をここで助けてください、私のコントローラクラスである:

@ManagedBean(name = "fileDownLoadController") 
@RequestScoped 
public class FileDownloadController { 

private StreamedContent downloadFile; 

public FileDownloadController() { 
    InputStream stream = null; 
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext(); 



     File result = new File(extContext.getRealPath("//WEB-INF//temp//process.png")); 

     File outFile = new File("process.png"); 
     Logger.getLogger(FileDownloadController.class.getName()).log(Level.SEVERE, outFile.getAbsolutePath()); 

     stream = extContext.getResourceAsStream(result.getAbsolutePath()); 

     downloadFile = new DefaultStreamedContent(stream, "image/png", "process.png"); 

} 

public StreamedContent getFile() { 
    return downloadFile; 
} 

public void setFile(StreamedContent file) { 

     this.downloadFile = file; 

} 

ビューコード:

   <h1 class="title">FileDownload</h1> 

      <div class="entry"> 
       <p>FileDownload </p> 

       <h:form> 

        <p:commandButton value="Download" ajax="false" > 
         <p:fileDownload value="#{fileDownloadController.file}" /> 
        </p:commandButton> 

       </h:form> 

      </div> 

ここでエラーメッセージです:

java.lang.NullPointerException 
at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:53) 
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) 
at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 

答えて

1

問題はstream = extContext.getResourceAsStream(result.getAbsolutePath())です。このファイルは、あなたが言及したもう1つの場合と同様に、このクラスパスにはないことに注意してください。下を見て、それは動作するはずです -

public FileDownloadController() throws FileNotFoundException {   
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();   
    File result = new File(extContext.getRealPath("//WEB-INF//temp//process.png"));   
    InputStream stream = new FileInputStream(result.getAbsolutePath()); 
    downloadFile = new DefaultStreamedContent(stream, "image/png", "process.png"); 
}