how to write a utility to download a file from a URL?

S

steveeli

I am new in Java land. I am trying to write a small utility to
download any files by giving its url, something like http://.../1.jpeg

Can someone point me to the class I need to use. I looked the sun web
sites example, which downloads html contents to a string buffer. How
do I handle images? Thanks.
 
A

Andrew Thompson

steveeli said:
I am new in Java land. I am trying to write a small utility to
download any files by giving its url, something like http://.../1.jpeg

Can someone point me to the class I need to use.

It depends in what sense you need to use it,
if, by use, you mean display to the user, you
might try..

JLabel( (Icon)ImageIcon(URL) )
 
C

Chris Smith

steveeli said:
I am new in Java land. I am trying to write a small utility to
download any files by giving its url, something like http://.../1.jpeg

Can someone point me to the class I need to use. I looked the sun web
sites example, which downloads html contents to a string buffer. How
do I handle images? Thanks.

You'd use java.net.URLConnection. The only difference between
downloading general binary versus text is that you don't want to store
the data in a StringBuffer. Instead, you'd read from an InputStream
(see URLConnection.getInputStream).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jared Dykstra

I am new in Java land. I am trying to write a small utility to
download any files by giving its url, something like http://.../1.jpeg

Can someone point me to the class I need to use. I looked the sun web
sites example, which downloads html contents to a string buffer. How
do I handle images? Thanks.

Are you doing this just for fun? Or because you need a tool to
download stuff? If it is the later, why not simply use a tool like
wget which does the job (and more) quite nicely and freely. If it's
the former, the info you need has already been posted.

Java has an interesting concept of treating everything like a URL.
Files, database connections, files stored in a JAR file, everything.
If you're bored, try creating a new File object but give it a
"http://www.yadayada.com/somefile.txt" parameter instead of a local
path and see what happens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top