How to make a simple JSF download?

Asked

Viewed 959 times

1

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

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

I wouldn’t want to use any components like <p:fileDownload>, because I need something very simple and the <p:fileDownload> would have to create a logic to be able to identify the types: zip, xls, doc and etc.

Like 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 example that could help me?

  • Oops, I posted an answer, but I would like to ask a question, are you only having problems with the jsf part, or are you having problems with the Java part too? Because if so, put the java structure of my download method.

  • Oops, I saw you didn’t disable ajax in your code. I don’t use href, but I couldn’t find parameters to activate and disable ajax in it, I think this may be your problem. If this is the case, let me know. I edited my reply to mention this point.

  • What the method defineCaminhoDownload returns? A URL, a file path on the server?

  • Marcelo, I suspended the question because we already have three answers that do not solve your problem, on the contrary, pass away. If you still need help, please rephrase the question and make it clearer exactly what is on this link than you do overall. Particularly, I think there is a misconception in this implementation, that is, it gives the impression that you want to generate a URL (link) for a local file on the server. This is not possible, have to have a code that reads the file and is exactly what the <p:fileDownload> ago.

  • Another thing, with the <p:fileDownload> you do not need to make a logic for each file type (although this would be better for system usability). You can force the download of any file as in this answer.

3 answers

1

I don’t use the href but I took a look and apparently it does not accept as a parameter anything related to ajax, that needs to be disabled. See if this would be your problem. Otherwise, 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 method download does the java part (creates or prepares the file, if applicable).

0


I believe 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;  
}  
  • How does this answer the question? This code acts as a client by downloading, but does not solve the server problem!

-1

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

  • Hello, Gilson. Welcome to [en.so]! I think you misunderstood the question. The author of the question wants to download a file from the server and not from the table data, which is what the <dataExport> ago.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.