我正在开发一个JSF / PrimeFaces项目,我从数据库生成一些文件并将它们放在我项目的web / resources文件夹中。我正在使用ap:datable将文件名动态加载到网页中。数据表已激活singleMode。我想要的是根据用户选择使文件可下载。当选择文件并按下下载按钮时,抛出异常;
以下是我使用的代码:
<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 );
}
}
@ManagedBean(name = "gen")
@SessionScoped
public class GeneratorController implements Serializable {
private List<String> outFileNames;
public GeneratorController() {
outFileNames = new ArrayList<String>();
outFileNames.add("/resources/images/optimus.jpg");
outFileNames.add("/resources/images/optimus_1.jpg");
outFileNames.add("/resources/images/optimus_2.jpg");
outFileNames.add("/resources/images/optimus_3.jpg");
}
public List<String> getOutFileNames() {
return outFileNames;
}
public void setOutFileNames(List<String> outFileNames) {
this.outFileNames = outFileNames;
}
}
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
答案 0 :(得分:1)
如果文件系统中某处有一个外部文件,则必须使用新的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();
答案 1 :(得分:0)
我终于找到了下载文件的方法。图像所在的文件夹不在类路径上,所以
null
返回InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(selectedFileName);
并发生NPE。所以用
cmake
webapp上下文用于查找文件并且可以正常工作