0% found this document useful (0 votes)
4 views4 pages

Ex 2

The document outlines a Java program that creates a TCP socket to download a web page, specifically an image file. It includes an algorithm detailing the steps to read the file from a specified URL and save it to the current working directory. The program demonstrates the use of Java's URL class and handles exceptions during the download process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Ex 2

The document outlines a Java program that creates a TCP socket to download a web page, specifically an image file. It includes an algorithm detailing the steps to read the file from a specified URL and save it to the current working directory. The program demonstrates the use of Java's URL class and handles exceptions during the download process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex.No.

2 Write a HTTP web client program to download a web page using TCP
sockets.

Aim:
To write a program in java to create a socket for HTTP for web page
download.

Algorithm:
1. Start the program
2. Read the file to be downloaded from webpage
3. To download an image, use java URL class which can be found under
java.net package.
4. The file is downloaded from server and is stored in the current working
directory.
5. Stop the program

Program
Download.java

import java.io.*;
import java.net.URL;
public class Download
{
public static void main(String[] args) throws Exception
{
try
{
String fileName = "Sunflower-field-Fargo-North-
Dakota.jpg";
String website = "https://cdn.britannica.com/84/73184-
004-E5A450B5/"+fileName;
System.out.println("Downloading File From: " + website);
URL url = new URL(website);
InputStream inputStream = url.openStream();
OutputStream outputStream = new
FileOutputStream(fileName);
byte[] buffer = new byte[2048];
int length = 0;
while ((length = inputStream.read(buffer)) != -1)
{
System.out.println("Buffer Read of length: " +
length);
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
Output

Downloading File From:


http://tutorialspoint.com/java_dip/images/digital_image_p
rocessing.jpg
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1097
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1744
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1440
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 548
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1863
The downloaded file (Stored in the current working directory)

You might also like