This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes How to download file throuch datahandler object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to download file throuch datahandler object?" Watch "How to download file throuch datahandler object?" New topic
Author

How to download file throuch datahandler object?

Kush Sahu
Greenhorn

Joined: Jan 08, 2013
Posts: 1
I want to download a file in my jsf project. At bean side I am getting object of DataHandler. I tried to search many times but I did not found anything related to it. How can I download a file through datahandler object?

DataHandler src = attachment.getAttachment();

ByteArrayOutputStream output = new ByteArrayOutputStream();
src.writeTo(output);
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseHeader("Content-Type", src.getContentType());
externalContext.setResponseHeader("Content-Disposition", "attachment;filename=sachin.jpg");
externalContext.getResponseOutputStream().write(output.toByteArray());
facesContext.responseComplete();

And if anybody knows about a good tutorial on file download and file upload which describes whole process in detail would be great for me.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

I know good tutorial on uploading files. It works for me
http://balusc.blogspot.com/2007/04/imageservlet.html


True person is moral, false is right!
 
 
subject: How to download file throuch datahandler object?
 
Similar Threads
Open a new window programmatically
sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ
JSF-portlet request response object in jsp
how we forward a JSF page into Servlet
What's the best way to download files from a web app?