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
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(); } } }
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
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); } }
A file called googleHomePage.html will be generated in the specified location with the content same as that of the home page of google.