You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Thinh Doan <td...@twjconsulting.com> on 2001/12/11 16:44:28 UTC

Files download

I've seen discussions and examples on file upload.  They were very helpful.
Thank you.

And now for the return trip, are there any examples out there for files
download?  Appreciate any suggestions and examples.

Thank you,

Thinh


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Files download

Posted by Thinh Doan <td...@twjconsulting.com>.
Thanks Daniel.  Almost missed it.  Will give it a try and let you know.

Thinh

-----Original Message-----
From: Daniel WAMARA 2 [mailto:dwamara@hotmail.com]
Sent: Friday, December 14, 2001 2:54 AM
To: Struts Users Mailing List; tdoan@twjconsulting.com
Subject: Re: Files download


I know none but I made an application doing that, all you have to do is to
set up the content type of the response. Here's the code I made for :

public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException
{
    HttpSession session = request.getSession();

    try
    {
        <some codes here>
        String username     = getUser(request);
        <some codes here>
        String filetoget     = request.getParameter("filetoget");
        response.setContentType("text/html");
        response.setHeader("Content-disposition", "attachment; filename="+
filetoget);
        BufferedOutputStream bos = new
BufferedOutputStream(response.getOutputStream());
        FileInputStream fis = new FileInputStream(new File(filetoget));
        byte[] buffer = new byte[1024];
        int bytes_read;
        while((bytes_read = fis.read(buffer)) != -1)
            bos.write(buffer, 0, bytes_read);
        fis.close();
        bos.close();

    }
    catch(Exception me)
    {
        <some codes here>
    }

    return mapping.findForward("success");
 }



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Files download

Posted by Daniel WAMARA 2 <dw...@hotmail.com>.
I know none but I made an application doing that, all you have to do is to set up the content type of the response. Here's the code I made for :

public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException 
{
    HttpSession session = request.getSession();
    
    try 
    {
        <some codes here> 
        String username     = getUser(request);
        <some codes here>     
        String filetoget     = request.getParameter("filetoget");
        response.setContentType("text/html");
        response.setHeader("Content-disposition", "attachment; filename="+ filetoget);
        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        FileInputStream fis = new FileInputStream(new File(filetoget));
        byte[] buffer = new byte[1024];
        int bytes_read;
        while((bytes_read = fis.read(buffer)) != -1)
            bos.write(buffer, 0, bytes_read);
        fis.close();
        bos.close(); 

    }
    catch(Exception me)
    {
        <some codes here>
    }
    
    return mapping.findForward("success");
 }