0

Hi I am trying to implement pdf file download functionality by this way .

/* test.xhtml*/

<p:commandButton   actionListener="#{backingBean.download}" update=":form"/>

/BackingBean.java/

public Class BackkingBean implements serializable{
@ManagedBean
@ViewScoped

public void download(){
String uri = "http://173.24.57.274:8080/ROOT/html/xml/Test new_02.pdf";
URL url = new URL(uri);
File destination = new File("C:/Documents and Settings/microsoft/My Documents/Downloads/sample.pdf");
FileUtils.copyURLToFile(url, destination);
}

}

But when I am clicking on download button I am getting below error.

Caused by: java.io.IOException: Server returned HTTP response code: 505 for URL: http://173.24.57.274:8080/ROOT/html/xml/Test new_02.pdf

I have gone some stackoverflow posts regarding this error but did'nt find any solution which work for me.

What I am missing? How can overcome from this problem.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Java
  • 2,451
  • 10
  • 48
  • 85
  • I think this post answers your question: [How to stream a file download in a JSF backing bean?](http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean) – wobblycogs Jun 19 '13 at 16:08
  • It's probably because you are using a `p:commandButton` which defaults to using AJAX by default -- you can have a file downloaded with AJAX normally. Does using the URL directly in the browser gives you the same error? – Elias Dorneles Jun 19 '13 at 16:09
  • `elias`No if I try to type same url in my local browser it simply opens that pdf file for me.But this file should be able to download from anywhere I will use this application. – Java Jun 19 '13 at 16:14
  • `wobblycogs` I am going through this post but where I can provide my file path? They have just used filename. – Java Jun 19 '13 at 16:19
  • @elias: you're right, but the particular exception is thrown during `FileUtils.copyURLToFile()` step, long before JSF was able to return some response. – BalusC Jun 19 '13 at 16:22
  • 1
    @Ran: apart from the HTTP 505 problem, are you absolutely well aware that your code in its current form saves the file at the disk file system of the physical machine where the webserver runs and not the physical machine where the webbrowser runs? In other words, if you intented to save the file at enduser's disk file system, then your current approach completely fails when the webserver runs at a physically different machine than the webbrowser (as is the case in real world). Look at the link of wobblycogs for the right way of forcing a "Save As" dialogue with the desired file content. – BalusC Jun 19 '13 at 16:24
  • @BalusC oh my! that's the sort of thing one misses when giving 33% of attention! thanks for pointing it out. – Elias Dorneles Jun 19 '13 at 16:27
  • @BalusC : Yes I want to download a this file to client's location .What should I need to do for that.My files are stored under tomcat's ROOT folder. – Java Jun 19 '13 at 16:28
  • @Ran: oh wait, you're using a portlet. That answer is then indeed applicable. Google around for answers covering `PortletResponse` or so. I can't answer from top of head as I've never really used portlets. But to the point, your current attempt really doesn't do what you think it does. – BalusC Jun 19 '13 at 16:28
  • @BalusC : Yes I am using liferay-jsf portlet. So what will be way to achieve this download file functionality? – Java Jun 19 '13 at 16:29
  • Well, I wish you much luck then. At least, you should now understand that your initial approach makes no utter sense. You should write the file to the HTTP response, not to the server's disk file system. – BalusC Jun 19 '13 at 16:30
  • @Balusc :Yes after all these above explanations I am agree with you. – Java Jun 19 '13 at 16:31
  • @BalusC : So is your post http://stackoverflow.com/questions/2633002/handling-downloads-in-java will work for me? – Java Jun 19 '13 at 16:35
  • @Ran: No. It doesn't send the file to the client. It saves the file on the machine where the Java code runs (thus, in your case the webserver machine). You still have to send the file to the client (thus, in your case the enduser with the webbrowser). The save step is completely unnecessary if you just write immediately to the HTTP response. – BalusC Jun 19 '13 at 16:51
  • This may give some insight: http://www.liferay.com/web/raymond.auge/blog/-/blogs/801426 – BalusC Jun 19 '13 at 17:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32053/discussion-between-ran-and-balusc) – Java Jun 20 '13 at 05:59
  • Similar post in the Liferay Faces forums: http://www.liferay.com/community/forums/-/message_boards/message/25797383 – Neil Griffin Jun 21 '13 at 21:56

1 Answers1

0

You should try and encode the URL value. If I spy correctly, there seems to be a Space value within the name of the PDF file. i.e. "Test new_02.pdf". Encoding the URL using URLEncoder can eliminate issue with "special characters" within the URL and it is good practice to encode URL in java applications

Take a look at http://docs.oracle.com/javase/6/docs/api/java/net/URLEncoder.html

And read the documentation carefully to figure out how to encode the space character.

myqyl4
  • 301
  • 1
  • 4
  • 2
    That *may* fix the HTTP 505 problem, but that absolutely doesn't solve the concrete functional requirement of streaming a file download. – BalusC Jun 19 '13 at 16:29