How to Download File in Java
-
Download a File Using the
java.nio
Package in Java -
Download a File Using
FileUtils.copyURLToFile()
in Java -
Download a File Using
Files.copy()
in Java
This article teaches us how we can download a file in Java using various methods. Below, we will see four ways to download a file in Java. Our main focus is only to download a file from an URL that we will use in the examples.
Download a File Using the java.nio
Package in Java
The first example in this tutorial uses the java.nio
package that is just an improved and advanced alternative to Java’s traditional java.io
. The nio
part stands for New Input/Output
, which says that it has better capabilities than the former package. java.nio
provides new networking methods that we can use.
In the following code, first, we create a URL
object fetchWebsite
with the URL from where we want to download our file. Next, we construct a channel that reads the stream from fetchWebsite
, and we do that by calling the newChannel()
method of the Channels
class. To create a stream we call openStream()
from fetchWebsite
.
Now, we create a FileOutputStream
object fos
that creates a file locally with the name specified. Finally, we get the file from the online source using the transferFrom()
method. This method transfers the data from source to a FileChannel
that writes into the fos
. transferFrom()
takes three arguments, first is the readableByteChannel
object, second is the position from where we want to start writing the file, i.e., 0th position and lastly the maximum number of bytes to transfer.
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class DownloadFile {
public static void main(String[] args) throws IOException {
URL fetchWebsite = new URL(
"https://theswissbay.ch/pdf/Gentoomen%20Library/Programming/Java/Introduction%20to%20Java%20IO.pdf");
ReadableByteChannel readableByteChannel = Channels.newChannel(fetchWebsite.openStream());
try (FileOutputStream fos = new FileOutputStream("C:\\Users\\Downloads\\IntroToJava.pdf")) {
fos.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
}
}
}
Download a File Using FileUtils.copyURLToFile()
in Java
The next best way to download a file from an online source is to use the FileUtils.copyUrlToFile()
method included in the Apache Commons-IO library.
We use the following code to include the dependency in the project.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
Below, we create a URL
object with the link to the online file resource. Next, we create a local file where the downloaded file can reside. To do this, we make an object of the File
class and pass the file’s name with the extension into its constructor.
At last, we call the copyURLToFile()
method from the FileUtils
class that takes two arguments: the URL
object and the file
object.
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class DownloadFile {
public static void main(String[] args) throws IOException {
URL fetchWebsite = new URL(
"https://theswissbay.ch/pdf/Gentoomen%20Library/Programming/Java/Introduction%20to%20Java%20IO.pdf");
File file = new File("JavaIo.pdf");
FileUtils.copyURLToFile(fetchWebsite, file);
}
}
Download a File Using Files.copy()
in Java
Just like the first example, we use the java.nio
package but to call a different method. copy()
is a method of the Files
class. As the function’s name suggests, it copies a file to the specified target.
In the code, we have a URL
object fetchWebsite
that points to the source of the file. We create an object of Path
that tells the target where we want the file to be copied. Next, we open a stream to get the bytes from the online resource and pass the inputSteam
to copy()
. As it takes three arguments, the first and second are the inputStream
and path
objects, while the third specifies the CopyOption
or the way the copy operation should be performed. We use StandardCopyOption.REPLACE_EXISTING
to replace the file if it already exists.
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class DownloadFile {
public static void main(String[] args) throws IOException {
URL fetchWebsite = new URL(
"https://theswissbay.ch/pdf/Gentoomen%20Library/Programming/Java/Introduction%20to%20Java%20IO.pdf");
Path path = Paths.get("JavaIO.pdf");
try (InputStream inputStream = fetchWebsite.openStream()) {
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
}
}
}
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn