• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to download file from server through servlet

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic