This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSF and the fly likes File downloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "File downloading" Watch "File downloading" New topic
Author

File downloading

Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Hello,

I am writing a JSF web app with ICEfaces. The business part is implemented behind a session bean that acts as a facade.
The user of this web app should be able to download files (txt, csv, pdf).
How can I do that ? I have no idea ...

Thanks for your help,

Olivier
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just add a h:outputLink pointing to the URL of the file. If those files are to be retrieved from the database or from the filesystem in the server machine outside the webapplication, then create a servlet which does the task independently. Just let it take the file name or ID as parameter or pathinfo of the request, read the file as an InputStream and write it to the OutputStream of the response. Don't forget to set at least the content-type, content-length and content-disposition headers correctly. Set the content-disposition to 'attachment' to get a save-as dialogue.
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Thanks for your reply.

I think we will go to the servlet solution, as the document will be generated on the fly.
I think I can write the servlet, but I have problems to "connect" it !
Suppose, for example that the user click a button to download the file.
Inside the btn.action method I need to call the servlet. How to do that ? How to construct to Request ?
And how to send back the Reply to the user (and staying in the same page ...) ?

Thanks,

Olivier
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just map the servlet in the web.xml on a certain url-pattern the usual way.
You will stay at the same page. The page won´t be changed if you send a file as attachment.
You can find here an example, you may find it useful: http://balusc.blogspot.com/2007/07/fileservlet.html
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Thanks, it works (at least from an outputLink !

just a little question:
How can I call this servlet from a button action ?
Inside the action, i have tried:
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
RequestDispatcher dispatcher = request.getRequestDispatcher("FileDeliveryServlet");
dispatcher.forward(request, response);
But I get an exception
java.lang.UnsupportedOperationException: Use navigation rules instead
at com.icesoft.faces.webapp.http.servlet.ServletEnvironmentRequest.getRequestDispatcher(ServletEnvironmentRequest.java:342)
at fnwebb2bpresentation.SessionBean.getPlanningMenuItem_action(SessionBean.java:245)

Any idea ?

Thanks,

Olivier
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Use a plain HTML form pointing to the servlet.
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Thanks !
I can not
The customer wants a button ...
:-(
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Uh yes. Do you know HTML?

Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
I think I know HTML !
;-)
but I am in an ICEfaces page, with a menuBar composed of menuItem btns ...
When user clicks on the menuItems btn then, I want to call the servlet ...
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Well, then stream the file directly in bean action method. The HttpServletResponse is just available by ExternalContext#getResponse(). Don´t forget to call FacesContext#responseComplete() afterwards to inform JSF that you already took control over the response so that it won´t execute the render response phase. You may get some ideas out of this: http://balusc.blogspot.com/2006/05/pdf-handling.html
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Thanks for the link, Bauke

I have implemented (in fact cut and paste!) your solution.
When I click into the menu or use a command link, nothing happen ...
I have put traces into the code, and the sequence looks ok. The file is read and copied to the output buffer.
And facesContext.responseComplete() is also called.
I do not understand what is happening. Perhaps ICEfaces ?

Olivier
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Cut? I hope you just copied it

I don't have practical experience with ICEfaces, so I can't say much about this behaviour. All what I know is that it is an ajaxical component library. Did you use plain h:commandButton or some ice:commandComponent with ajaxical powers? If so, do you realize that you have to fire a normal HTTP request rather than an ajaxical request?
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Ooop, copy not cut !!!

Your code works without any problem with the Woodstock framework ...
So I have to investigate to the ICEfaces side.

Thanks,

Olivier
 
GeeCON Prague 2014
 
subject: File downloading