Problemas na geração de arquivo

1 resposta
rmsites

Amigos,

Eu acabo de ler o tutorial sobre geração de arquivos (Aprendendo a manipular arquivos) de Guilherme Silveira, e tentei fazer um teste, gerando um arquivo no servidor e enviar um link para download deste arquivo, mas tem algo errado.. Alguém pode me ajudar?

Vejam os fontes: 1) Formulário que chama o Bean que gera o arquivo:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
    
    <body>
        <f:view>
            <h:form>
                <h:panelGrid columns="1" border="0" cellspacing="5">
                    <h:commandLink id="geraArq" title="Gerar o arquivo de Folha" value="Gera o arquivo" action="#{mbProcessFile.geraArq}" />
                    
                </h:panelGrid>
            </h:form>
        </f:view>
        
        <a href="FormUpload.jsp">Back</a>
    </body>
</html>
2) Formulário que apresenta o resultado:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>   
    <body>
        <f:view>          
            <h:panelGrid columns="2" border="0" cellspacing="5">
                <h:outputText value="Result:"/>
                <h:outputLink value="http://localhost:8084/TimeCard/saida.txt" id="arq" target="http://localhost:8084/TimeCard/saida.txt" />
            </h:panelGrid> 
        </f:view>        
        <a href="FormUpload.jsp">Back</a>
    </body>
</html>

3) E, o Bean que gera o arquivo (somente os métodos)

public String geraArq() {
        TimeCardFileProcessor tcfp = new TimeCardFileProcessor();
        try{
        tcfp.geraArq();
        } catch (Exception ex){
            ex.printStackTrace();
            return "erro";
        }
        return "gravou";
    }

    public String geraArq() throws IOException {

        FileWriter writer = new FileWriter("saida.txt", true);
        PrintWriter saida = new PrintWriter(writer,true);
        saida.println("teste1");
        saida.println("teste2");
        saida.print("teste final");
        saida.close();
        writer.close();
        return "gravou";
    }

O que está faltando? Onde está o meu erro?
Ah, e claro, tem alguma forma mais inteligente de fazer isso?

obrigado.

1 Resposta

rmsites

Pessoal, eu consegui avançar. Eu gero o arquivo, e pego o link onde ele está, mas não estou conseguindo exibi-lo no JSP, podem me ajudar?

Veja o Bean corrigido:
private  URI arqDownload;
    
    public String geraArq() throws IOException {
        File arq = new File("saida.txt");
        if (arq.exists()) {
            arq.delete();
        }
        arq.createNewFile();
        FileWriter writer = new FileWriter(arq, true);
        PrintWriter saida = new PrintWriter(writer, true);
        saida.println("teste1");
        saida.println("teste2");
        saida.print("teste final");
        URI url = arq.toURI();
        this.setArqDownload(url);
        saida.close();
        writer.close();
        return "gravou";
    }

E o JSP onde eu espero o resultado:

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>   
    <body>
        <f:view>          
            <h:panelGrid columns="2" border="0" cellspacing="5">
                <h:outputText value="Result:"/>
                <h:outputLink title="link" target="#{mbProcessFile.arqDownload}" value="Pronto"  />
                
            </h:panelGrid> 
        </f:view>        
        <a href="FormUpload.jsp">Back</a>
    </body>
</html>

O que tem errado com o outputLink? Ele não exibe nada....

obrigado,
Reginaldo.

Criado 3 de setembro de 2008
Ultima resposta 4 de set. de 2008
Respostas 1
Participantes 1