Webpage Download

Algorithm
1. Get URL from the user.
2. Create a file instance to store the downloaded page.
3. Download the page using java URL methods.
4. View the download page
5. Stop

import java.io.*;
import java.net.*;
class MyDownload
{
public void Download() throws Exception
{
try
{
String WebPage, MyPage;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
// URL Instance
System.out.print("Enter the URL : ");
WebPage = br.readLine();
URL url = new URL(WebPage);
// File Instance
System.out.print("Enter filename to store : ");
MyPage = br.readLine();
File Out = new File(MyPage);
FileOutputStream FOS = new FileOutputStream(Out);

// Dowload the page
InputStream in = url.openStream();
byte buf[] = new byte[1024];
int i, len;
while( (len = in.read(buf)) > 0 )
{
for(i = 0; i < len; i++)
{
FOS.write((char)buf[i]);
}
}
// Close the streams
in.close();
FOS.close();
}
catch (MalformedURLException M)
{
System.out.println(M);
}
catch (Exception E)
{
System.out.println(E);
}
}
}
class DownloadPage
{
public static void main(String args[]) throws Exception
{
String Choice;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
MyDownload MDP = new MyDownload();
MDP.Download();
System.out.println("Download complete. View the file");
}
}
Output
$ javac DownloadPage.java
$ java DownloadPage
Enter the URL : http://www.google.co.in
Enter filename to store : mypage.html
Download complete. View the file

Leave a comment