# How to download and save a file from Internet in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 11, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-download-save-file-from-internet

> A quick article to learn how to download and save a file from the Internet in Java.

In this article, we will look at different **ways to download and save a file from the Internet** in Java.

## Using Java NIO API

In Java 7+, the simplest and a pure Java-based solution is using the NIO API (classes in `java.nio.*` package)  to download and save a file from a URL. Here is an example that downloads and saves an image from a URL to a file on the local file system:

```java
try {
    // internet URL
    URL url = new URL("https://i.imgur.com/mtbl1cr.jpg");

    // download and save image
    ReadableByteChannel rbc = Channels.newChannel(url.openStream());
    FileOutputStream fos = new FileOutputStream("cat.jpg");
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

    //close writers
    fos.close();
    rbc.close();

} catch (IOException ex) {
    ex.printStackTrace();
}
```

The `transferFrom()` method is by far more efficient than using a simple loop for copying bytes from the source channel to this channel. The third argument in `transferFrom()` is the maximum number of bytes to transfer. `Long.MAX_VALUE` will transfer at most 2^63 bytes.

## Using `InputStream` Class

Another JDK-only solution to download and save an Internet file is using the `InputStream` class. You can use `File.openStream()` to open an `InputStream` and then [convert it into a file](https://attacomsian.com/blog/java-convert-inputstream-to-file) by using `Files.copy()` method:

```java
try (InputStream in = URI.create("https://i.imgur.com/mtbl1cr.jpg")
        .toURL().openStream()) {

    // download and save image
    Files.copy(in, Paths.get("cat.jpg"));

} catch (IOException ex) {
    ex.printStackTrace();
}
```

## Using Apache Commons IO

The [Apache Commons IO](https://commons.apache.org/proper/commons-io/) library provides `FileUtils.copyURLToFile()` method to download and save a file from the Internet as shown below:

```java
try {
    // internet URL
    URL url = new URL("https://i.imgur.com/mtbl1cr.jpg");

    // local file path
    File file = new File("cat.jpg");

    // connection and read timeouts
    // TODO: adjust as per your own requirement
    int connectionTimeout = 10 * 1000; // 10 sec
    int readTimeout = 300 * 1000; // 3 min

    // download and save file
    FileUtils.copyURLToFile(url, file, connectionTimeout, readTimeout);

} catch (IOException e) {
    e.printStackTrace();
}
```

Don't forget to include Apache Commons IO dependency to your Maven's project `pom.xml` file:

```xml
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
```

For a [Gradle project](https://attacomsian.com/blog/install-gradle-on-ubuntu), add the following dependency to your `build.gralde` file:

```groovy
implementation 'commons-io:commons-io:2.6'
```

## Further Reading

You may be interested in other Java I/O articles:

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
