//
// ConnectURL.java
//

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


public class ConnectURL
{
   public static void main (String[] args)
   {
      // Check the arguments: we expect, at least, the url code.
      if (args.length != 2)
      {
         System.out.println("Usage: java ConnectURL <url> <destination file>");
         return;
      }

      URLConnection connection;
      URL urlLocation;

      try
      {
         urlLocation = new URL(args[0]);     
         connection = urlLocation.openConnection();    
         System.out.println("Connection is: " + connection.getClass().getName());
         System.out.println("Content type:  " + connection.getContentType());
         System.out.println("Data        :  " + connection.getContent().toString());

         FileOutputStream outputStream = new FileOutputStream(args[1]);
         InputStream inputStream = connection.getInputStream();

         int data;
         while (true)
         {
            data = inputStream.read();
            if (data == -1)
               break;
            outputStream.write(data);
         }

         outputStream.close();
         inputStream.close();

      }
      catch (Exception e)
      {
         System.out.println("Caught exception. ");
         System.out.println(e.getMessage());
         e.printStackTrace();
         return;
      }
   }
}  // End of the class
