Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to download file throuch datahandler object?

 
Kush Sahu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know good tutorial on uploading files. It works for me
http://balusc.blogspot.com/2007/04/imageservlet.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic