This example shows how to download a web page in Java.
We can download and save a webpage from given URL to an HTML file in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import javax.swing.text.html.parser.DTD; public class MainClass { public static void main(String[] args) { try { URL u = new URL("http://www.insecure.in"); OutputStream out = new FileOutputStream("test.htm"); InputStream in = u.openStream(); DTD html = DTD.getDTD("html"); System.out.println(html.getName()); in.close(); out.flush(); out.close(); } catch (Exception e) { System.err.println("Usage: java PageSaver url local_file"); } } } |