File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Aique Fernandez
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you access the files?
 
Gokul Nanthakumar
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. Be sure to set the content-length also, just in case the client needs it.

Bill
 
Mark E Hansen
Ranch Hand
Posts: 650
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code snippet :-
(But I am assuming you have a fixed file path)

 
Manish Singh
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@prasenjit.svnit das the complete code should not be provided.
The suggestions given by Gokul is enough
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic