Javatpoint Logo
Javatpoint Logo

How to Download a File from a URL in Java?

Downloading a file from a URL in Java is a relatively simple task, but there are a few different ways to do it. In this blog post, we will discuss two of the most common methods: using the URL and HttpURLConnection classes, and using the Apache Commons IO library.

What is downloading a file using a URL?

Downloading a file through a Java code using a URL allows the Java application to download a file directly into a local system from a remote repository or any other local storage. This process reads a file from the URL and writes it to a local file. Java offers three different ways to download a file using a URL.

1: Plain Java structure:

If we use Java without using any external library, it takes the file as input and reads those data byte by byte. Now, if we take the byte-by-byte data from an input stream and write the bytes to a file output stream, we can achieve downloading using URL.


File downloaded successfully?

2: Using Java.IO package: is the traditional Java package that contains various classes. It has some built-in classes used explicitly for reading &writing to a stream.

We have to import the URLConnection, FileOutputStream, IOException, InputStream, and OutputStream. Within the main(), create a OutputStream and InputStream object and two string variables to hold the URL link and file location. Within the try block, set the URL and the URLConnection using getInputStream(). The following catch block will handle any input-output exception and execute the printStackTrace(). The finally block (which executes automatically as a mandatory part of the program) will display the message "URL's File downloaded."

Steps to download a file.

  • Create a URL object for the file we want to download.
  • Open a connection to the URL using the HttpURLConnection class.
  • Get the input stream for the connection.
  • Create an output stream to save the file to disk.
  • Read bytes from the input stream and write them to the output stream until the end of the file is reached.
  • Close all streams and connections.

Let's implement the above steps in a Java program.


URL's File downloaded....

3: Using NIO:

Java NIO (abbreviated as New IO) is an alternative input-output Java API that also comes as a Java package. The NIO acts as an alternative to the standard Java IO and Java Networking API. While using the Java IO library, the streams read the data byte by byte. But in the Java NIO package, data are read as channels and buffers.

We have to import the URL, Channels (which is a part of NIO package), FileOutputStream, IOException, InputStream OutputStream, and Now, within the Main class, we have created the main(). Inside the main() and within the try block, we have created two String objects by the name fileLink and oppath where we have defined the URL link and the file location. Then we have created an input stream for the file we want to download. Then, we have to produce a new channel responsible for reading the data from this input stream. Next, we have to create an output stream that will write the file contents after putting it from the channel object. Now, we have to fetch the channel from this output stream & define its contents from the channel. The following catch block will handle any input-output exception and execute the printStackTrace().

4: Using Apache Commons IO:

Apache Commons IO is a utility package of Java that has an class. It comprises a copyURLToFile method that can help program IO operations. This method takes 2 arguments - The first is the object that points to the source file while the second is the object pointing to the output file path. Note that both paths should consist filename at the end. The output path should be the file location on your local system from where the file will get downloaded.

First, we have to import Within the Main class, we have to create the main() inside which we have to create two String variables to hold the URL link and file location. Now, use the FileUtils.copyURLToFile() method to make the program download a file (from the specified location) using the URL (given). This method is taking two parameters fileLink and oppath, that we have created earlier.


File downloaded successfully!

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA