# Multiple Ways to Download Files from Internet in Java

It's very common we need to download files from Internet and there are several ways to do in Java. Let me introduce them.

# Java IO

We use BufferedInputStream to read and FileOutputStream to write to the local file:

private static void javaIO() {
    try {
        try (BufferedInputStream in = new BufferedInputStream(new URL(URL).openStream());
             FileOutputStream fileOutputStream = new FileOutputStream("pkslow.io.html")) {
            byte dataBuffer[] = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                fileOutputStream.write(dataBuffer, 0, bytesRead);
            }
        } catch (Exception e) {
            // handle exception
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

# Java NIO 1

Code as below:

private static void javaNIO1() {
    try {
        URL website = new URL(URL);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream("pkslow.nio.html");
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

# Java NIO 2

We use Files.copy, code as below:

private static void javaNIO2() {
    try {
        URL website = new URL(URL);
        try (InputStream in = website.openStream()) {
            Files.copy(in, Paths.get("pkslow.nio2.html"), StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

# Apache Common IO

Add the dependency:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

We use FileUtils.copyURLToFile method to download:

private static void apacheCommonIO() {
    try {
        URL website = new URL(URL);
        FileUtils.copyURLToFile(website, new File("pkslow.commonsIO.html"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

# OKHttpClient

Add the dependency:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.10.0</version>
</dependency>

Use FileOutputStream to save the response body:

private static void okHttpClient(String downloadUrl) {
    try {
        OkHttpClient client = new OkHttpClient();
        okhttp3.Request request = new okhttp3.Request.Builder().url(downloadUrl).build();
        okhttp3.Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            throw new IOException("Failed to download file: " + response);
        }
        FileOutputStream fos = new FileOutputStream("pkslow.okHttpClient.html");
        fos.write(response.body().bytes());
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

# AsyncHttpClient

Add the dependency:

<dependency>
    <groupId>org.asynchttpclient</groupId>
    <artifactId>async-http-client</artifactId>
    <version>2.12.3</version>
</dependency>

Use FileOutputStream to save the file:

private static void asyncHttpClient() {
    try {
        AsyncHttpClient client = Dsl.asyncHttpClient();
        FileOutputStream stream = new FileOutputStream("pkslow.asyncHttpClient.html");
        client.prepareGet(URL).execute(new AsyncCompletionHandler<FileOutputStream>() {
            @Override
            public State onBodyPartReceived(HttpResponseBodyPart bodyPart)
                    throws Exception {
                stream.getChannel().write(bodyPart.getBodyByteBuffer());
                return State.CONTINUE;
            }

            @Override
            public FileOutputStream onCompleted(Response response)
                    throws Exception {
                return stream;
            }
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

# Code

Please check the code in GitHub (opens new window).


References:

Download a File with OkHttp (opens new window)

Top 5 ways to Download a File from any given URL in Java? (opens new window)

Download a File From an URL in Java (opens new window)

Last Updated: 8/18/2023, 11:39:36 PM