首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NPE on filedownload in primefaces

NPE on filedownload in primefaces
EN

Stack Overflow用户
提问于 2011-08-12 18:47:09
回答 1查看 2.7K关注 0票数 0

我有一个与这个File Download Does Not Work类似的问题。然而,我已经尝试了所有建议的答案,但都没有用。我还是能拿到NEP。有人能帮帮忙吗?

下面是我的控制器类:

代码语言:javascript
运行
复制
@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;

}

查看代码:

代码语言:javascript
运行
复制
                <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>

以下是错误消息:

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2011-08-13 14:20:00

问题出在stream = extContext.getResourceAsStream(result.getAbsolutePath())上。请注意,在本例中,该文件不在类路径中,就像您提到的另一个一样。看下面,它应该行得通-

代码语言:javascript
运行
复制
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");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7039009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档