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

how to download file from server through servlet

 
subodh varshney
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to download a file from server through servlet using inputstream.
if any one have any code plz help me.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are your requirements? You just want them to make a request to a servlet and the servlet sends them the file?

There was a similar discussion here, but the solution I quite liked was to use the RequestDiospatcher. You may need to set the filename and content type first.

Otherwise you can read from a FileInputStream and write to the OutputStream, but you would still need the filename and content type to be set.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is better to continue conversations here rather than emailing people. You were lucky, I usually don't check that address for days at a time...

--------------------------------------
From email:
i have download this code but i don't know how to use this.



I have formatted the code and cleaned it a little. There are some key problems with this code, I suggest you don't use it.

The bits you are interested in look like this:
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essentially you set the content type and 'filename' on the response. The lines for these should be obvious. You use a FileInputStream of some other type of stream to suck in a file, then pump it down the OutputStream. When you run out of data, stop.

Actually there is a bug in the bos.write(bytesRead) line, that's not right at all. Icky code.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file is available on the context, you could also try:


I haven't tried this, but it appeals to me.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic