download pdf that is out of the jsf java application

0

I need to download some pdf files that are outside of my application, they are in the folder c: \ tmp \

How could you make use of primefaces to make these files available for download?

I thought about saving in folder C: \ jboss-as-7.1.1.Final \ standalone \ deployments \ app.war \ WEB-INF

but every time I deploy this file is deleted

    
asked by anonymous 23.11.2015 / 20:06

1 answer

1

Try this:

Declare an attribute of type DefaultStreamedContent in your bean: Example:

private DefaultStreamedContent download;

Get the Get and sets:

public DefaultStreamedContent getDownload() {
        return download;
    }

public void setDownload(DefaultStreamedContent download) {
        this.download = download;
}

Next:

    //Método que faz o download do anexo
    public void prepDownload() throws Exception {
        File file = new File("c:/tmp/arquivo.pdf");

        InputStream input = new FileInputStream(file);
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    }

Then on your button;

<p:commandButton icon="ui-icon-arrowthickstop-1-s"
    title="Download Anexo"
    actionListener="#{bean.prepDownload()}"
    ajax="false">
    <p:fileDownload value="#{bean.download}" />
</p:commandButton>
    
23.11.2015 / 20:40