tags:

views:

68

answers:

1

in java how the url is downloaded and save it in to a local directory. More over i want a offline view of that downloaded url(especially the html content).

+1  A: 

Here's some code for sucking HTML into strings. Note, this doesn't pull the content (images, etc...), just the HTML! Enjoy :)

try
{
    URL url = new URL("http://www.stackoverflow.com");
    URLConnection connection = url.openConnection();

    connection.setDoInput(true);
    InputStream inStream = connection.getInputStream();
    BufferedReader input = new BufferedReader(new InputStreamReader(inStream));

    String html = "";
    String line = "";
    while ((line = input.readLine()) != null)
    {
        html += line;
    }

    //Now you can do what you please with
    //the HTML content (save it locally, parse, etc...)
}
catch(Exception e)
{
    //Error handling
}
Reed Olsen
I'd recommend using Jakarta's HTTPClient over the Java IO classes, the former dealing with redirects, etc automatically.
Nick Holt