Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Downloading files

Thread view: 
odwrotnie - 16 Mar 2006 23:29 GMT
Hi,

how can I save file from www to my HDD by java?

Signature

Best regards,
Odwrotnie.

Roedy Green - 16 Mar 2006 23:37 GMT
>how can I save file from www to my HDD by java?

use the Download method of the FileTransfer class.

See http://mindprod.com/products1.html#FILETRANSFER
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

opalpa@gmail.com opalinski from opalpaweb - 16 Mar 2006 23:54 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.