File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File downloading

 
Olivier Scalbert
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a plain HTML form pointing to the servlet.
 
Olivier Scalbert
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !
I can not
The customer wants a button ...
:-(
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh yes. Do you know HTML?

 
Olivier Scalbert
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic