
Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
import java.net.*;
import java.io.*;
/**
Class to download contents of URL.
*/
final public class GetFromInternet {
private URL url = null;
public GetFromInternet(URL url) {
this.url = url;
}
public void to(OutputStream o) throws IOException {
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","");
InputStream in = conn.getInputStream();
int b = -1;
while ((b = in.read()) != -1)
o.write(b);
in.close();
}
public void to(String fname) throws FileNotFoundException,
IOException {
to(new FileOutputStream(fname));
}
public static void main(String args[] ) throws IOException {
new GetFromInternet(new URL(args[1])).to(new
FileOutputStream(args[0]));
}
}
compile and then run something like:
java GetFromInternet it.html "http://www.google.com"
the example above will get google's splash source and put it into file
named "it.html"
Glancing at the code it looks right, I tried it, ... I do see a
problem... should the output stream be closed? It should be before
main exits, but I did not do that. Roedy's solution is more robust,
take a look at that one. The code above has gist.
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 17 Mar 2006 00:01 GMT
I made the close improvement. No primises that code does not have
subtle bugs. Again, look at Roedy's solution; his code is more
considered.
import java.net.*;
import java.io.*;
/**
Class to download contents of URL.
*/
final public class GetFromInternet {
private URL url = null;
public GetFromInternet(URL url) {
this.url = url;
}
public void to(OutputStream o, boolean closeOutput) throws
IOException {
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","");
InputStream in = conn.getInputStream();
int b = -1;
while ((b = in.read()) != -1)
o.write(b);
in.close();
if (closeOutput)
o.close();
}
public void to(String fname) throws FileNotFoundException,
IOException {
to(new FileOutputStream(fname), true);
}
public static void main(String args[] ) throws IOException {
new GetFromInternet(new URL(args[1])).to(args[0]);
}
}
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
odwrotnie - 17 Mar 2006 11:47 GMT
Thank You very much!

Signature
Best regards,
Odwrotnie.