How to make a simple download in JSF? [closed]

1

How to make a simple JSF download? I have a list of attachments in <p:dataTable> and would like to implement a simple download.

I implemented with <a href> , the link on the file name was created, but when I click it it just does not download.

I would not like to use any component like <p:fileDownload> , because I need something very simple and <p:fileDownload> would have to create a logic to be able to identify types: zip, xls, doc and etc.

As I said, I tried <a href> as below, the link appears but nothing happens when I click on the link:

<p:column headerText="Nome do Anexo" width="55%">
   <a href="${cadastroAtividadeBean.defineCaminhoDownload(anexo.nomeAnexo)}"
                                    title="${anexo.nomeAnexo}">
        <h:outputText value="#{anexo.nomeAnexo}" />
   </a>
</p:column>

Would anyone have any examples that could help me?

    
asked by anonymous 09.11.2015 / 13:17

3 answers

0

I think the method below meets your need.

public static File gravaArquivoDeURL(String stringUrl, String pathLocal) {  
    try {  

        //Encapsula a URL num objeto java.net.URL  
        URL url = new URL(stringUrl);  

        //Queremos o arquivo local com o mesmo nome descrito na URL  
        //Lembrando que o URL.getPath() ira retornar a estrutura   
        //completa de diretorios e voce deve tratar esta String  
        //caso nao deseje preservar esta estrutura no seu disco local.  
        String nomeArquivoLocal = url.getPath();  

        //Cria streams de leitura (este metodo ja faz a conexao)...  
        InputStream is = url.openStream();  

        //... e de escrita.  
        FileOutputStream fos = new FileOutputStream(pathLocal+nomeArquivoLocal);  

        //Le e grava byte a byte. Voce pode (e deve) usar buffers para  
        //melhor performance (BufferedReader).  
        int umByte = 0;  
        while ((umByte = is.read()) != -1){  
            fos.write(umByte);  
        }  

        //Nao se esqueca de sempre fechar as streams apos seu uso!  
        is.close();  
        fos.close();  

        //apos criar o arquivo fisico, retorna referencia para o mesmo  
        return new File(pathLocal+nomeArquivoLocal);  

    } catch (Exception e) {  
        //Lembre-se de tratar bem suas excecoes, ou elas tambem lhe tratarão mal!  
        //Aqui so vamos mostrar o stack no stderr.  
        e.printStackTrace();  
    }  

    return null;  
}  
    
09.11.2015 / 13:34
1

I do not use href but I took a look and apparently it does not accept anything related to ajax , which needs to be disabled. See if this would be your problem. If not, try my suggestion below.

You can do it this way (I simplified it well, but you can understand how it works):

<p:dataTable id="dataTable" var="list" value="#{seuBean.suaLista}"
  <p:column>
    <p:commandButton ajax="false" title="#{msg['migracao.download']}"
            value="Download" action="#{seuBean.download(list)}" />
  </p:column>
</p:dataTable>

Where the download method does the java part (creates or prepares the file, if any).

    
09.11.2015 / 13:32
-1

If you want to make a simple download, just use the dataExport of primefaces. Then just specify the format: XLS, PDF or etc.

    
06.12.2015 / 20:19