Servelt download *.xls

4 respostas
felipe.brentan

Boa tarde,

Pessoal estou com problema,

Sou novo no java, estou tentando fazer um download de um arquivo xls, para q apareça aquela tela de download, eu fiz a planilha pelo POI quero q o meu usuario clique em abaixar a planilha e la fique salve no pc dele ... atravez do download ...

Sei q tem q fazer servelt ...

eu fiz o codigo queria q alguem me desse um apoio !

public class GeraPlanilha extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {  
        InputStream is  = getClass().getClassLoader().getResourceAsStream(&quot;C:/Estatistica.xls&quot;);   
        
        byte[] b = inputStreamToBytes(is);
        
        ServletOutputStream out = response.getOutputStream();                             
        
        response.reset();   
        response.setHeader(&quot;Content-Disposition&quot;, &quot;attachment;filename=Estatistica.xls&quot;);   
        response.setContentType(&quot;Application/xls&quot;);   
        response.setContentLength(b.length);   
               
        out.write(b);   
        out.flush();   
        out.close();    
     
    }

    public byte[] inputStreamToBytes(InputStream in) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[1024];
        int len;

        while((len = in.read(buffer)) &gt;= 0)
        out.write(buffer, 0, len);

        in.close();
        out.close();
        return out.toByteArray();
        } 
    
}

Agradeço se alguem puder me ajudar.

4 Respostas

joaosouza

Olá felipe.brentan blz,

Ocorre algum erro, problema ?

Para que possamos te ajudar…

Falow !!

T

Não é “Application/xls” e sim “application/vnd.ms-excel”.

Veja http://www.duke.edu/websrv/file-extensions.html para uma lista das extensões e dos “content-types” oficiais.

felipe.brentan

Bom cara …

eu estou usando o oc4j …

dai aparece so q a um erro na programação …

esta certo este codigo, eu fiz pelo q li na net

Coloquei isso no servelt codigo anterior e coloquei este no jsp;

Clique no link para fazer o download da planilha template.
[a href = ‘geraPlanilha.do’/]
[img src=“C:…” alt="" width=“10” height=“10” /]Download do arquivo

vlww …

T

Quando você usa “getResourceAsStream” não pode especificar um nome de arquivo dessa forma “C:\Estatistica.xls”.

Ele tem de ser algo que está na sua aplicação Web, no formato “/Estatistica.xls”.

Criado 7 de fevereiro de 2008
Ultima resposta 7 de fev. de 2008
Respostas 4
Participantes 3