4

Hi i have implemented the example given in primefaces showcase for FileDownload

When I render page and when i click on the download button for the first time I am able to download the file.But when click on download 2nd time it's giving me following exception.

 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.

I am pasting my code here 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;
    }  
}

My xhtml file

<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) How can i make it work and what is the exact problem.

2) I have files on one of our shared drive so when get stream by "getResourceasStream" I am getting null stream.For this i am directly using FileInputStream to read the file.is this correct method?

3)If i have 3 files to download do I need to write 3 <p:fileDownload> tags?

mdp
  • 815
  • 4
  • 17
  • 32
  • I am able to solve the above Read Error by using this post in stackoverflow 'http://stackoverflow.com/questions/924990/how-to-cache-inputstream-for-multiple-use?lq=1' – mdp Sep 11 '12 at 03:24

3 Answers3

1

line 16 look the line 16 from demo

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

You need to access the resource by jsf.

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

Because in Java EE you don't have permission for accessing the file directly from the root path.

Nimpo
  • 430
  • 6
  • 13
  • But I can't get all those files into my project.Cause that is a large file system.what other ways that i can achieve this? – mdp Sep 11 '12 at 16:08
0

You have to create another method, then copy the code and paste into the constructor the new method.

In the download button to call the new method with the action attribute

Sorry for my English

0

if you file it's null,you should get the file or download. here an example:

private UploadedFile file;
private StreamedContent archivoDescargar;
//method get
public StreamedContent getArchivoDescargar() throws IOException {
    if(file != null) 
        return archivoDescargar = new DefaultStreamedContent(this.file.getInputstream(),this.file.getContentType(), this.file.getFileName());
    else
        return archivoDescargar;    
}
David Hackro
  • 3,652
  • 6
  • 41
  • 61