wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to download file from www adress to local folder? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to download file from www adress to local folder?" Watch "How to download file from www adress to local folder?" New topic
Author

How to download file from www adress to local folder?

Imre Ipolyi
Greenhorn

Joined: Jan 28, 2005
Posts: 5
Hi,

I'm a total beginner with java, so this question may look very easy to answer for you

How can I dowload a picture from the web to a folder on my computer e.g.
the file http://maps.wunderground.com/data/640x480/2xeu_ws_anim.gif
to the folder D:\network ?

Once again, I'm a beginner. Today is the day when I first time used other then simple html code in a file (copy-paste & change a bit) and I didn't know how did I do it but it worked as I wanted
So now I'm glad

Hey, thanx if you answer with The Answer!

Imre
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Heres a quick and dirty, you can expand on this for something more robust.



I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Imre Ipolyi
Greenhorn

Joined: Jan 28, 2005
Posts: 5
Hi,
thanks for the code!
I guess I made a weak try to use it and I did put it in the wrong place so it didn't work.
Does it have to be at least similar to this?

----------------------------------------------------------------------------

<html><head><link rel="stylesheet" href="content_data/prj.css" type="text/css">
<title>Weather Maps Europe</title></head>

<script language="javascript">
try{
URL url = new URL("http://maps.wunderground.com/data/640x480/2xeu_ws_anim.gif");
URLConnection urlConn = url.openConnection();
BufferedInputStream is = new BufferedInputStream(urlConn.getInputStream());
File out = new File("D:\\network", "2xeu_ws_anim.gif");
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(out));
byte[] b = new byte[8 *1024];
int read = 0;
while((read = is.read(b)) > -1){
bout.write(b,0, read);
}
bout.flush();
bout.close();
is.close();

}
catch(IOException mfu){
mfu.printStackTrace();
}
</script>

<body id="contentMerck" leftmargin="0" topmargin="0" background="index_files/content.htm"
bgcolor="#ffffff" link="#cc0000" marginheight="0" marginwidth="0" text="#000000" vlink="#990000">
<div id="content" style="width: 0%; height: 0%;">
<layer name="contentNS"></layer>

<TABLE cellpadding=0 cellspacing=0 border=0>
<TR>
<TD height=256 width=640 valign=top>
<TABLE class=smalltableheader cellspacing=1 cellpadding=0 border =0 width="100%"><TR>
<TD bgcolor="#000080" width="100%">
<font color="#FFFFFF"><b>Europe Wind<BR>
</b></font>
</TD></TR></TABLE>
<center class=smalltableheader>
<b>Click on map for current conditions and forecasts.</b><br>
<A HREF="Wind_anim.html">
<IMG SRC="D:\network\2xeu_ws_anim.gif" BORDER=0
WIDTH=640 HEIGHT=480
ALIGN=TOP ALT="Weather Map" usemap="#IMAGEMAP">
</a>
</TD></TR></TABLE>
</div></body></html>

----------------------------------------------------------------------------

Thanks for the review!
Imre

[ January 31, 2005: Message edited by: Imre Ipolyi ]

[ January 31, 2005: Message edited by: Imre Ipolyi ]
[ January 31, 2005: Message edited by: Imre Ipolyi ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Imre -

Welcome to JavaRanch.

Java is not JavaScript. The two are not interchangable! You must compile Java into an Applet if you want to include it in a Web page.

Also, for future posts in which you include code, please be sure to enclose the code in the CODE UBB tags, which are just below the text area where you type your message.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jeff Bosch:
You must compile Java into an Applet if you want to include it in a Web page.

This particular problem won't work as an Applet, either. Applets cannot access the local file system. Most likely, you will need to compile this as a Java application instead. You will need to download the Java SDK from http://java.sun.com to do this. However, if you want to do this in JavaScript instead, then you should visit the HTML and JavaScript forum here at the Ranch.

HTH

Layne


Java API Documentation
The Java Tutorial
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Layne -

I wasn't trying to intimate that the code would work as an applet. My main point was that the code cannot run as a script. Considering the misuse of Java code as though it were JavaScript, I reasoned that going any deeper would be totally lost on Imre.
Imre Ipolyi
Greenhorn

Joined: Jan 28, 2005
Posts: 5
Hi Carl, Jeff and Layne,

thanks for your answers, Jeff you are right that it was no sense to go deeper in the explanation when I even didn't know that there was a difference between Java and JavaScript, but it's great that now I know this info and it's also great that Layne told me about the JavaScript forum.
Does JavaScript need some kind of program like SDK or is it used simply like the html code? If it can be used as a simple code in a html file, I'd rather prefer to use JavaScript and not Java. Hmmm... Do I understand right that Java is a programming language like c++ and JavaScript is just an "internet code"?
Are these two very similar to eachother?

Hey, Layne, thanks for the link to the JavaScript forum!
Imre
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to download file from www adress to local folder?