• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Downloading Content from the Internet

October 9, 2007 //  by Krishna Srinivasan//  Leave a Comment

Let us write a Simple Downloader in this techincal tip by making use of the classes with java.net package. URL stands for Uniform Resource Locator and it is used to locate a resource in the Web in a standard fashion. A resource in the Web can be anything; it can be as simple as Html Document or can be a complex multimedia content such as audio/video file. For example,www.javabeat.net represents a URL which is a simple Html Resource. Likewise, http://java.com/en/download/windows_xpi.jsp?begindownload=true represents a URL which is a downloadable file resource.

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

In Java, an URL is represented by the URL class. Another API that is closely associated with the URL class is the URLConnection object. It represents an open connection to the target resource. Given below is the complete code listing for the Simple Downloader.

ContentDownloader.java

[code lang=”java”] package tips.network.download;

import java.io.*;
import java.net.*;

public class ContentDownloader {

private String url;
private static final int SIZE = 1024;
private String destinationFile;

public ContentDownloader(String url){
this.url = url;
}

public void downloadContentsTo(String destinationFile){
this.destinationFile = destinationFile;

URL urlObject = null;
try {
urlObject = new URL(url);
}catch (MalformedURLException e) {
e.printStackTrace();
}

URLConnection urlConnection = null;
InputStream inputStream = null;
BufferedInputStream bufferedInput = null;

FileOutputStream outputStream = null;
BufferedOutputStream bufferedOutput = null;

try{
urlConnection = urlObject.openConnection();
inputStream = urlConnection.getInputStream();
bufferedInput = new BufferedInputStream(inputStream);

outputStream = new FileOutputStream(this.destinationFile);
bufferedOutput = new BufferedOutputStream(outputStream);

byte[] buffer = new byte[SIZE];
while (true){
int noOfBytesRead = bufferedInput.read(buffer, 0, buffer.length);
if (noOfBytesRead == -1){
break;
}
bufferedOutput.write(buffer, 0, noOfBytesRead);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
closeStreams(new InputStream[]{bufferedInput, inputStream},
new OutputStream[]{bufferedOutput, outputStream});
}
System.out.println("Downloading completed");
}

private void closeStreams(
InputStream[] inputStreams, OutputStream[] outputStreams){

try{
for (InputStream inputStream : inputStreams){
if (inputStream != null){
inputStream.close();
}
}
for (OutputStream outputStream : outputStreams){
if (outputStream != null){
outputStream.close();
}
}
}catch(IOException exception){
exception.printStackTrace();
}
}
}

[/code]

The above class has a constructor that accepts the URL of the resource whose content we wish to download. The section of interest is the downloadContentsTo() method which accepts the name of the file in the local machine to which the content of the URL has to be stored. Now, let us look into the implementation of the method. The code tries to represent the URL string that we passed as a URL object. If the URL string is not valid, then a MalformedURLException will be thrown.

It then tries to open a connection to the URL object by calling the URL.openConnection() method which returns a InputStream object. We have decorated the returned input stream to the BufferedInputStream object for faster reading from the underlying stream. We read the contents of the input stream and at the same time write the contents to the output stream as pointed by the destination file.

Now, let us run the above program, by having the following test class,

ContentDownloaderTest.java

[code lang=”java”] package tips.network.download;

public class ContentDownloaderTest {

public static void main(String[] args) {

String url = "http://www.google.com";
String destinationFile = ‘C:\googleHomePage.html’;

ContentDownloader downloader = new ContentDownloader(url);
downloader.downloadContentsTo(destinationFile);
}
}
[/code]

A file called googleHomePage.html will be generated in the specified location with the content same as that of the home page of google.

Category: JavaTag: Core Java

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Creating user defined exceptions
Next Post: Generation of Random Numbers »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact