gpt4 book ai didi

primefaces - 第二次单击primefaces下载时读取错误

转载 作者:行者123 更新时间:2023-12-01 05:29:05 26 4
gpt4 key购买 nike

嗨,我已经为 FileDownload 实现了在 primefaces 展示中给出的示例

当我呈现页面时,当我第一次点击下载按钮时,我可以下载文件。但是当第二次点击下载时,它给了我以下异常。

 javax.faces.FacesException: java.io.IOException: Read error
at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241)
at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:191)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1815)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:198)
at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:71)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:51)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:344)
at javax.faces.component.UICommand.broadcast(UICommand.java:103)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:978)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:275)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1289)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:716)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
... 21 more.

我在这里粘贴我的代码 FileDownloadController.java
@ManagedBean(name="fileDownloadController")
@SessionScoped
public class FileDownloadController {

private StreamedContent file;

public FileDownloadController() {
File fileabc=new File("C:/temp/velocitypdf.pdf");
InputStream stream=null;
try {
stream = new FileInputStream(fileabc);
file = new DefaultStreamedContent(stream, "application/pdf", "velocity.pdf");
stream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public StreamedContent getFile() {
return this. file;
}
}

我的 xhtml 文件
<h:form>
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">
</p:dialog>
</h:form>
<h:form id="form">

<h:commandLink value="Download" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)">
<p:fileDownload value="#{fileDownloadController.file}" />
</h:commandLink>


</h:form>

1)我怎样才能让它工作,确切的问题是什么。

2)我在我们的一个共享驱动器上有文件,所以当通过“getResourceasStream”获取流时,我得到的是空流。为此,我直接使用 FileInputStream 来读取文件。这是正确的方法吗?

3)如果我有 3 个文件要下载,我需要写 3 <p:fileDownload>标签?

最佳答案

line 16 look the line 16 from demo



InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).
getResourceAsStream("/images/optimusprime.jpg");

需要通过jsf访问资源。

(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext("your path in project")

因为在 Java EE 中,您没有直接从根路径访问文件的权限。

关于primefaces - 第二次单击primefaces下载时读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12356331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com