aspose file tools*
The moose likes Servlets and the fly likes how to download file from server through servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to download file from server through servlet" Watch "how to download file from server through servlet" New topic
Author

how to download file from server through servlet

subodh varshney
Greenhorn

Joined: Aug 01, 2005
Posts: 11
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

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
 
subject: how to download file from server through servlet