Primefaces 上的 NPE 文件下载
我遇到了与此类似的问题文件下载不起作用。然而,我已经尝试了所有建议的答案,但没有成功。我仍然得到新经济政策。有人可以帮忙
这是我的控制器类:
@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)
I have a similar problem to this File Download Does Not Work. However, I have tried all the answers suggested but to no avail. I still get a NEP. Can someone please help
Here is my controller class:
@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;
}
View code:
<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>
Here is the error message:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在
stream = extContext.getResourceAsStream(result.getAbsolutePath())
上。请注意,在这种情况下,该文件不像您提到的另一种情况那样位于类路径中。看下面,它应该可以工作 -Problem is with
stream = extContext.getResourceAsStream(result.getAbsolutePath())
. Note that the file is not in the classpath in this case like in the other one you mentioned. Look below, it should work -