Download de arquivo .ZIP usando Struts

3 respostas
R

Galera, é o seguinte: tenho um sistema J2EE utilizando Struts, criei uma pasta no projeto onde consta o manual do projeto. Criei um botão para fazer o download, só que sempre q coloco um link ele vai buscar no Struts - Config o PATH, tem algum codigo q faça eu fazer do download do arquivo?

valeu galera

3 Respostas

P

Qual versão do struts vc esta usando ?

se vc estiver usando 1.2.6 ou superior, vc pode usar a action DownloadActtion

http://struts.apache.org/1.x/struts-extras/apidocs/org/apache/struts/actions/DownloadAction.html
http://wiki.apache.org/struts/StrutsFileDownload

R

“pm”:
Qual versão do struts vc esta usando ?

se vc estiver usando 1.2.6 ou superior, vc pode usar a action DownloadActtion

http://struts.apache.org/1.x/struts-extras/apidocs/org/apache/struts/actions/DownloadAction.html
http://wiki.apache.org/struts/StrutsFileDownload

tem algum codigo para me passar?

abraços

P

No wiki tem varios exemplos...

mport java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;


public class FileDownloadAction extends DownloadAction {
    protected Logger log=Log.getLogger(FileDownloadAction.class.getSimpleName());
/*
 * (non-Javadoc)
 * @see org.apache.struts.actions.DownloadAction#getStreamInfo(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
    protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1,
            HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        
        String filePath = arg2.getParameter("uri");
        
        StringBuffer buffer = new StringBuffer();
        String path = null;
        if (filePath != null) {
            String http = ConfigConstants.getResourceBundle().getString(
                    "HTTP_URL");
            buffer.append(http);
            buffer.append(filePath);
            
            path=buffer.toString();
        }
        
        log.finest("Path name is :" + path);
        String contentType = "application/x-msexcel";
        
        return new SI(contentType, new URL(path));
    }
    
    
    /**
     *
     * @author nsuj
     *
     */
    public static class SI implements StreamInfo {
        
        private String contentType;
        
        private URL file;
        
        public SI(String contentType, URL file) {
            this.contentType = contentType;
            this.file = file;
        }
        
        public String getContentType() {
            return this.contentType;
        }
        
        /**
         * Method to get a stream on the file to download
         *
         * @return The InputStream wrapping the file to download
         *
         */
        public InputStream getInputStream() throws IOException {
            return file.openStream();
        }
        
    }
    
}

[url]http://forum.java.sun.com/thread.jspa?threadID=655374&messageID=4120857[/url]

Criado 8 de agosto de 2006
Ultima resposta 8 de ago. de 2006
Respostas 3
Participantes 2