Friday, March 18, 2011

How to download content from a URL ?

To download content from a URL, you may need to study two classes :
java.net.URL
java.net.URLConnection

The following sample illustrates how to download content from "http://www.google.com"

The content would be saved in the file "output.htm"

Comments are welcome.


/******************************************************************************
* File : Downloader.java
* Author : http://java.macteki.com/
* Description :
*   Download the contents of a URL
* Tested with : JDK 1.6
******************************************************************************/

public class Downloader
{
  public static void main(String[] args) throws Exception
  {
    // prepare to get input from URL
    java.net.URL url = new java.net.URL("http://www.google.com");
    java.net.URLConnection con = url.openConnection();

    java.io.InputStream is=con.getInputStream();

    // prepare to write output
    String output_file="output.htm";
    java.io.FileOutputStream os=new java.io.FileOutputStream(output_file);

    int BUF_SIZE=1024;
    byte[] buffer = new byte[BUF_SIZE];
    
    while (true)
    {
      int n = is.read(buffer);  // read from URL
      if (n>0) os.write(buffer,0,n);     // write to file
      if (n<0) break;    // finished reading
    }
  }
}

No comments:

Post a Comment