Primefaces 上的 NPE 文件下载

发布于 2024-11-29 00:53:48 字数 2256 浏览 0 评论 0原文

我遇到了与此类似的问题文件下载不起作用。然而,我已经尝试了所有建议的答案,但没有成功。我仍然得到新经济政策。有人可以帮忙

这是我的控制器类:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-12-06 00:53:48

问题出在 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");
}

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 -

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文