java.lang.NullPointerException at org.primefaces.component.f



我正在一个JSF/PrimeFaces项目上工作,我正在从数据库生成一些文件,并将它们放在我的项目的web/resources文件夹中。我使用p:datable将文件名动态加载到网页中。数据表激活了singleMode。我想要的是使文件可下载基于用户的选择。当文件被选中并按下下载按钮时,将抛出异常;

下面是我使用的代码:

JSF页面

   <div id="content">
        <h:form id="resultForm">
            <center>
                <p:panel id="files" style="width: 80%">
                    <p:dataTable id="resultDT" var="file" value="#{gen.outFileNames}" 
                                 selection="#{downloader.selectedFileName}" selectionMode="single" rowKey="#{file}">
                        <f:facet name="header">
                            Generated Files
                        </f:facet>
                        <p:column headerText="File names">
                            <h:outputText value="#{file}" />
                        </p:column>
                        <f:facet name="footer">
                            <p:commandButton value="Download" ajax="false" icon="ui-icon-arrowthick-1-s">
                                <p:fileDownload value="#{downloader.file}"/>
                            </p:commandButton>
                        </f:facet>
                    </p:dataTable>
                </p:panel>
            </center>
        </h:form>
    </div>
<标题> 下载控制器
@ManagedBean(name = "downloader")
@ViewScoped
public class FilesDownloadController implements Serializable {
    private String selectedFileName;
    public StreamedContent getFile() {
        if (selectedFileName == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Download warning", "Please click a file and click on the download button."));
            return null;
        }
        System.out.println("Download 1 : selectedFileName = " + selectedFileName);
        System.out.println("Download 2");
        System.out.println("Download 3");
        InputStream stream = this.getClass().getResourceAsStream(selectedFileName);
        System.out.println("Download 4");
        StreamedContent file = new DefaultStreamedContent(stream, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getMimeType(selectedFileName), 
        selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
        System.out.println("Download 5 = " + selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
        System.out.println(file != null ? "File content OK " + file.getContentType() + " " + file.getName() : "File content NOK");
        return file;
    }
    public String getSelectedFileName() {
        return selectedFileName;
    }
    public void setSelectedFileName(String selectedFileName) {
        this.selectedFileName = selectedFileName;
        System.out.println("Selected File changed: " + this.selectedFileName );
    }
}
<标题>发电机bean h1> 抛出异常
        Infos:   Selected File changed: /resources/images/optimus_1.jpg
        Infos:   Download 1 : selectedFileName = /resources/images/optimus_1.jpg
        Infos:   Download 2
        Infos:   Download 3
        Infos:   Download 4
        Infos:   Download 5 = optimus_1.jpg
        Infos:   File content OK image/jpeg optimus_1.jpg
        FATAL:   JSF1073 : javax.faces.FacesException intercepté durant le      traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=null
        FATAL:   No associated message
javax.faces.FacesException
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
       at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
       at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
       at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
       at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
       at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
       at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
       at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:81)
       at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
       at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
       at javax.faces.component.UICommand.broadcast(UICommand.java:300)
       at javax.faces.component.UIData.broadcast(UIData.java:1108)
       at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
       at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    ... 31 more

如果你在文件系统的某个地方有一个外部文件,你必须使用new FileInputStream(new file (exportFile.getAbsolutePath()))而不是FacesContext.getCurrentInstance(). getexternalcontext (). getresourceasstream (exportFile.getAbsolutePath())。我在Primefaces 8中遇到了同样的问题,我是这样解决的:

file = DefaultStreamedContent.builder().name(exportFile.getName())
                  .contentType("application/zip").stream(() ->
      {
         try{
            return new FileInputStream(new File(exportFile.getAbsolutePath()));
                     } catch (FileNotFoundException e) {
                        e.printStackTrace();
                     }
                     return null;
                  }).build();

我终于找到了下载文件的方法。图像所在的文件夹不在类路径上,所以

InputStream stream = this.getClass().getResourceAsStream(selectedFileName);

返回null并发生NPE。通过将下载控制器的代码替换为

InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(selectedFileName);

webapp上下文用于定位文件,它起作用

最新更新