This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes File download servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "File download servlet" Watch "File download servlet" New topic
Author

File download servlet

C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Hi All,
Im trying to create a simple download servlet and try to retrieve from a html link. Upon clicking the link I only get a black DownloadServlet.do page opened.
Im trying to download the erp.pdf file which is placed direcly under the root directory.

Here's my servlet code:





web.xml description is as follows

the url link is :


Thanks in advance for you help.

[ September 28, 2008: Message edited by: Bear Bibeault ]

[ September 28, 2008: Message edited by: C Vivek ]
[ September 28, 2008: Message edited by: Bear Bibeault ]
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
I think the problem is here:


The path should not have a forward slash, else it will be treated as absolute path and not relative. It should be:
C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Thanks, but i still have a problem I hope you can help.
The following code shows me a blank page "http://.../DownloadFile.do?filename=erp" if I use IE but works file with Firefox or Google Chrome.

The problem is that the users of the Intranet site im makinf use IE only.

I have ensured though that the file path is right( It gives a NullPointerException otherwise)

Would be grateful for your reply.


link


servlet mapping in web.xml


and the servlet code


[ September 28, 2008: Message edited by: C Vivek ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41169
    
  45
I think IE tries to get the file type from the URL, and gets confused if there isn't any. (I know, it should use the content type header, but for some reason it doesn't. That's IE for you.) Try to make the URL look like a PDF file URL by appending "&iehack=.pdf" to it. I've seen other software packages do that successfully.


Ping & DNS - my free Android networking tools app
C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Im guilty of not looking around other threads before posting. Thought before anybody makes an effort to reply, I should inform ranchers that the problem is resolved.
I just had to add content-dispostition in the reponse header.(which I found in one of the previous posts)

Thank you Javaranch!
C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Thanks Ulf!! That worked smoothly too.

I have just used content-disposition in the response header to allow a download in the application.



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Satish Chilukuri:
It should be:
No. Page-relative paths are usually a big problem in web applications. If they work at all, they are fragile.

Rather, a context-relative path beginning with the / should be used. Please see the JSP FAQ entry on Resource URLs for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Thanks Bear!
Changed the code accordingly.
Anand Ramasamy
Greenhorn

Joined: Oct 30, 2009
Posts: 6
C Vivek wrote:Thanks Bear!
Changed the code accordingly.


Hi,
I have different problem after using the "content-disposition: attachement" header.
The problem is after clicking the open button in the dialogue box, the adobe reader reports 'This file cannot be opened' error.
The browser I am using is IE6.
I have noticed the behaviour that the pdf file is stored inside the temporary intenet folder and it is getting deleted immediately after clicking on the open button.

I checked in many forums and the advice is to use "Cache-control: public","Cache-control: max-age=60".
But still it is not working for me.
And I hv tried many other combinations
like "Cache-control: private" and "Pragma: cache"
"Content-Type: application/pdf", "'Content-Length: file->size"
"Content-disposition: attachment; filename="'. file->filename"
"Expires: 0', 'Pragma: cache', 'Cache-Control: private"


But still it is not working. The application is running in Wesphere6.1
Please advice me some solution
 
 
subject: File download servlet
 
Similar Threads
question on servlet-mapping
servlet programming problem in Eclipse
Http 404 Error
404 again!
Browser Path Not Refreshing