File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes What's the best way to download files from a web app? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "What Watch "What New topic
Author

What's the best way to download files from a web app?

Aique Fernandez
Greenhorn

Joined: May 04, 2010
Posts: 3
Hi!.

I'm developing a J2EE application and I'm a little stuck.

I use a CMS to store PDF files. I can access to this files from a servlet and obtain his content in a Stream, but I can't access to the path. I want to offer the possibility to download the file.

How can I do that?.

Thanks!.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

How can you access the files?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gokul Nanthakumar
Ranch Hand

Joined: May 11, 2010
Posts: 56
1. set your response content type to application/pdf (MIME type for pdf) (response.setContentType)
2. write the file stream to the response

when browser access your servlet, it will open download dialog box as the content type is not html.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
3. Be sure to set the content-length also, just in case the client needs it.

Bill
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
There's an Article in the ServletsFaq: http://faq.javaranch.com/java/ImageServlet
Note that the same methodology applies to downloading content of various types, including PDF files.
prasenjitkumar das
Greenhorn

Joined: Mar 17, 2010
Posts: 12
Try this code snippet :-
(But I am assuming you have a fixed file path)

Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
@prasenjit.svnit das the complete code should not be provided.
The suggestions given by Gokul is enough
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What's the best way to download files from a web app?
 
Similar Threads
Problem accessing an image file from a jar file
From where do we get .tld files
Accessing Files on a remote server
how to deal with the garbage data in JSP?
creating in memory files and folders