You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Patrick B Haggood <co...@email.com> on 2005/03/31 15:09:11 UTC

Download servlet example

Anyone have an example of downloading using MyFaces?  I see on the
sample you can use outputtext components, but that's using the real
filename (which I keep in a datastore to guard against duplicate
filenames; the system assigns new filename, FILE0002, FILE0003, etc
apon upload)

Thanks!



Re: Download servlet example

Posted by Patrick B Haggood <co...@email.com>.
Thanks for the example; it works great.  However, I too am having the
same problem with the subsequent clicks.  Any solution yet?

On Thu, 2005-03-31 at 07:18 -0600, Aaron Bartell wrote:
> Here is how I have done it based on things I have found on the net. 
...
> Any button or link that they click after I do the .responseComplete() it 
> doesn't work the first time. But the second time they click the button 
> or link it executes the appropriate code.  I am open to suggestions on 
> any errors in my code to fix that problem...



Re: Download servlet example

Posted by Heath Borders <he...@gmail.com>.
We've been using the ResourceServlet with great success here.  We had
to tweak it a little so that it would point to our base directory,
rather than the MyFaces base directory, but other than that, its been
great.

It would be nice if that was available under a more generic release.


On Thu, 31 Mar 2005 07:18:28 -0600, Aaron Bartell
<aa...@gmail.com> wrote:
> Here is how I have done it based on things I have found on the net. The
> nice thing about streaming it to the browser is that the file need not
> exist in your context, it can be anywhere on the file system. The only
> bummer is that this seems to create a problem after the user downloads.
> Any button or link that they click after I do the .responseComplete() it
> doesn't work the first time. But the second time they click the button
> or link it executes the appropriate code.  I am open to suggestions on
> any errors in my code to fix that problem...
> 
>    private void writeFileToBrowser(String fileSource, String
> downloadName, String contentType) {
> 
>        if (!FacesContext.getCurrentInstance().getResponseComplete() &&
> FileControl.exists(fileSource)) {
> 
>            HttpServletResponse response = (HttpServletResponse)
> FacesContext.getCurrentInstance()
>                    .getExternalContext().getResponse();
>            response.setContentType(contentType);
>            response.setHeader("Content-Disposition",
> "attachment;filename=\"" + downloadName + "\"");
> 
>            try {
>                ServletOutputStream out = response.getOutputStream();
>                BufferedInputStream bufferedinputstream = new
> BufferedInputStream(new FileInputStream(
>                        fileSource));
> 
>                byte abyte0[] = new byte[4096];
>                int i;
>                while ((i = bufferedinputstream.read(abyte0, 0, 4096))
> != -1)
>                    out.write(abyte0, 0, i);
> 
>                out.flush();
>                out.close();
> 
>                FacesContext.getCurrentInstance().responseComplete();
>            } catch (IOException ex) {
>                ex.printStackTrace();
>            }
>        }
>    }
> 
>    public static boolean exists(String fileSource) {
>        File f = new File(fileSource);
>        return f.exists();
>    }
> 
> HTH,
> Aaron Bartell
> 
> 
> Patrick B Haggood wrote:
> 
> >Anyone have an example of downloading using MyFaces?  I see on the
> >sample you can use outputtext components, but that's using the real
> >filename (which I keep in a datastore to guard against duplicate
> >filenames; the system assigns new filename, FILE0002, FILE0003, etc
> >apon upload)
> >
> >Thanks!
> >
> >
> >
> >
> >
> 


-- 
-Heath Borders-Wing
hborders@mail.win.org

Re: Download servlet example

Posted by Aaron Bartell <aa...@gmail.com>.
Here is how I have done it based on things I have found on the net. The 
nice thing about streaming it to the browser is that the file need not 
exist in your context, it can be anywhere on the file system. The only 
bummer is that this seems to create a problem after the user downloads. 
Any button or link that they click after I do the .responseComplete() it 
doesn't work the first time. But the second time they click the button 
or link it executes the appropriate code.  I am open to suggestions on 
any errors in my code to fix that problem...



    private void writeFileToBrowser(String fileSource, String 
downloadName, String contentType) {

        if (!FacesContext.getCurrentInstance().getResponseComplete() && 
FileControl.exists(fileSource)) {

            HttpServletResponse response = (HttpServletResponse) 
FacesContext.getCurrentInstance()
                    .getExternalContext().getResponse();
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", 
"attachment;filename=\"" + downloadName + "\"");

            try {
                ServletOutputStream out = response.getOutputStream();
                BufferedInputStream bufferedinputstream = new 
BufferedInputStream(new FileInputStream(
                        fileSource));

                byte abyte0[] = new byte[4096];
                int i;
                while ((i = bufferedinputstream.read(abyte0, 0, 4096)) 
!= -1)
                    out.write(abyte0, 0, i);

                out.flush();
                out.close();

                FacesContext.getCurrentInstance().responseComplete();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static boolean exists(String fileSource) {
        File f = new File(fileSource);
        return f.exists();
    }

HTH,
Aaron Bartell


Patrick B Haggood wrote:

>Anyone have an example of downloading using MyFaces?  I see on the
>sample you can use outputtext components, but that's using the real
>filename (which I keep in a datastore to guard against duplicate
>filenames; the system assigns new filename, FILE0002, FILE0003, etc
>apon upload)
>
>Thanks!
>
>
>
>  
>