Downloading files via HTTP

Hi there,

I need to download a file from an internet adress like
http://www.someserver.com/somedoc.pdf”.
Is there a short answer to solve this problem?
Have googled a lot, but don’t find, what I am looking for.

What I want: a simple class that ask for a URL and saves
the document (PDF in my case) on my harddisk.

Thank you, AdamP

http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLConnection.html ?

from www.rganon.com


 import java.io.*;
 import java.net.*;

 public class suckURL {
   String aFile;
   String aURL;

   public static void main(String args[]) {
     // GIF  JAVA How-to  at Real's Home
     String url = 
       "http://www.rgagnon.com/images/";
     suckURL b = new suckURL(url, "jht.gif");
     b.doit();
     }

   suckURL(String u, String s){ 
     aURL  = u;
     aFile = s;
     }

   public void doit() {
     DataInputStream di = null;
     FileOutputStream fo = null;
     byte [] b = new byte[1];  
       
     try {
       System.out.println("Sucking " + aFile);
       System.out.println("   at " + aURL );
       // input 
       URL url = new URL(aURL + aFile);
       URLConnection urlConnection = url.openConnection();
       urlConnection.connect();
       di = new DataInputStream(urlConnection.getInputStream());

       // output
       fo = new FileOutputStream(aFile);

       //  copy the actual file
       //   (it would better to use a buffer bigger than this)
       while(-1 != di.read(b,0,1)) 
         fo.write(b,0,1);
         di.close();  
         fo.close();                
         }
       catch (Exception ex) { 
         System.out.println("Oups!!!");
         ex.printStackTrace(); 
         System.exit(1);
         }
       System.out.println("done.");  
     }
 }


import java.io.*;
import java.net.*;

class Get
{

      public static void main( String[] args )
      {
            if (args.length == 0) {
                  System.out.println ("Usage  : Java Get <dokument>");
                  System.out.println ("Example: Java Get http://www.server.com/doc.pdf");
                  System.exit(1);
            }
            try
            {
                  URL url = new URL(args[0]);

                  InputStreamReader in = new InputStreamReader (url.openStream());
                  String name = args[0].substring (args[0].lastIndexOf ("/")+1);
                  FileOutputStream fos = new FileOutputStream (name);

                  char[] buf = new char[1024];
                  byte[] buf2 = new byte[1024];
                  int nread = 0;

                  System.out.println ("Lade Datei: "+name);
                  while ( ( nread = in.read (buf) ) > 0 ) {
                        for (int i=0; i<nread; i++)
                        buf2[i] = (byte)buf[i];
                        fos.write (buf2, 0 ,nread);
                  }
                  in.close();
                  fos.close ();
                  System.out.println ("Ready.");
            }
                  catch ( Exception e ) {
                  System.out.println("Error");
            }
      }
}

Thanks for all your help, I use the code above now!
Greetings, AdamP