Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

upload and download files using java

 
m luu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let me know how to upload and download files using java. Thanks in advance.
mluu
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To download directly from a URL:
- create a URL from the URL string
- create a URLConnection by sending openConnection to the URL
- on the connection, setUsesCaches to false, setDoInput to true, setDoOutput to false
- obtain the InputStream from the URLConnection
- copy the file using something like this:

(The IOUtil methods, part of a utility class here, simply ensure that the references aren't null before closing the streams.)
If you're looking to do this via servlets, that's a different solution.
-Jeff L.-
[ January 19, 2004: Message edited by: Jeff Langr ]
 
mylifeisjava javaislife
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did lot of research and found that using input type=file in jsp i could serach for the file on the client system.I could not make out what do I write on the server side(i.e. in my java class).
Please help me with some code or any links.Thanks for your help.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mylifeisjava javaislife:
I did lot of research and found that using input type=file in jsp i could serach for the file on the client system.I could not make out what do I write on the server side(i.e. in my java class).
Please help me with some code or any links.Thanks for your help.

Search for "file upload servlet" on Google. There appear to be many freely available source code examples, including one from O'Reilly. It's not a terribly difficult thing to do, nor is it trivial. It involves parsing a multipart/form-data request.
Good luck,
Jeff L.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic