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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kush Sahu
  • 399
  • 1
  • 13
  • 33
  • As to the `DataHandler` part, this is not recognizeable as part of JSF API. What API is that `DataHandler` class coming from? You should just lookup and read its documentation to fill in the missing parts. As to the JSF part, the question is already answered in http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean/9394237#9394237 – BalusC Jan 07 '13 at 11:57
  • @BalusC When I checked my page by firebug to see the response, I am getting that file. But I dont know why its not showing me "save as" dialog box of windows. – Kush Sahu Jan 07 '13 at 13:23
  • You mean that it's displaying it inline instead of as attachment? In other words, the download works perfectly fine, but the way how the browser deals with it is not correct? – BalusC Jan 07 '13 at 13:25
  • @BalusC This is what I am getting in response header Content-Disposition attachment;filename=sachin.jpg Content-Length 12259 Content-Type application/octet-stream;charset=UTF-8 Date Mon, 07 Jan 2013 11:51:09 GMT Etag "4e01ce5b" Liferay-Portal Liferay Portal Enterprise Edition 6.0 EE SP1 (Bunyan / Build 6011 / January 13, 2011) Server Apache-Coyote/1.1 X-JAVAX-PORTLET-FACES-NAM... true – Kush Sahu Jan 07 '13 at 13:28
  • You've still not elaborated in detail what exactly happens instead. Is the browser displaying it inline instead of as attachment? If so, then it's just a matter of browser configuration setting. JPG files are by default associated with Firefox application itself. – BalusC Jan 07 '13 at 13:32
  • @BalusC Actually I am beginner in web page development. I am not getting the term "inline" in this context. I will tell you whole thing what I understand in my project. I have commandLink for download and in bean side I have the above code. Whenever I am clicking that commanlink in server side its executing my download method correctly but at browser end its nt showing any dialog box to save it. Then I checked my webpage by firebug and in console I found that I am getting response header like above. – Kush Sahu Jan 07 '13 at 13:41
  • What are you seeing in the browser window? Is the browser showing a blank page or the sole image itself (thus, "inline")? – BalusC Jan 07 '13 at 13:43
  • @BalusC Ohh I got it. Thanx BalusC for your quick replies. Its not showing blank page or that image. Its showing the same screen where I have clicked that download link as nothing has happened.:( – Kush Sahu Jan 07 '13 at 13:51
  • Oh, are you sending an ajax request or so? – BalusC Jan 07 '13 at 13:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22325/discussion-between-kush-sahu-and-balusc) – Kush Sahu Jan 07 '13 at 13:52
  • @BalusC This is my code at jsf side. I just found that p:commandlink is by default ajax=true Is that the reason why I am not getting any dialog box? – Kush Sahu Jan 07 '13 at 14:00
  • Well, that was already answered in the bottom of the link I posted in my 1st comment. – BalusC Jan 07 '13 at 14:02

1 Answers1

0

One of my friend solve for me this. Actually liferay environment was the problem. Earlier I was setting a portlet response. So he made a different servlet for download.

Kush Sahu
  • 399
  • 1
  • 13
  • 33