我有一个与这个File Download Does Not Work类似的问题。然而,我已经尝试了所有建议的答案,但都没有用。我还是能拿到NEP。有人能帮帮忙吗?
下面是我的控制器类:
@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)发布于 2011-08-13 14:20:00
问题出在stream = extContext.getResourceAsStream(result.getAbsolutePath())上。请注意,在本例中,该文件不在类路径中,就像您提到的另一个一样。看下面,它应该行得通-
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");
}https://stackoverflow.com/questions/7039009
复制相似问题