Java IO Tutorial

Java – How to download a file from the Internet

This article shows you how to download a file from an URL by using the following methods :

  1. Apache Commons IO
  2. Java NIO

1. Apache Commons IO

1.1 This is still my prefer way to download a file from the Internet, simple and clean. Read the signature :

org.apache.commons.io.FileUtils

//int = number of milliseconds
public static void copyURLToFile(URL source, File destination,
            int connectionTimeout, int readTimeout) throws IOException

1.2 Full example.

HttpUtils.java

package com.mkyong;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class HttpUtils {

    public static void main(String[] args) {

        String fromFile = "ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest";
        String toFile = "F:\\arin.txt";

        try {

            //connectionTimeout, readTimeout = 10 seconds
            FileUtils.copyURLToFile(new URL(fromFile), new File(toFile), 10000, 10000);

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

    }
}

1.3 Maven

pom.xml

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

1.4 Gradle

build.gradle

dependencies {
    compile 'commons-io:commons-io:2.5'
}

2. Java NIO

2.1 Try Java 7 NIO example.


	URL website = new URL(fromFile);
	ReadableByteChannel rbc = Channels.newChannel(website.openStream());
	FileOutputStream fos = new FileOutputStream(toFile);
	fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
	fos.close();
	rbc.close();

2.2 Full example.

HttpUtils.java

package com.mkyong;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class HttpUtils {

    public static void main(String[] args) {

        String fromFile = "ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest";
        String toFile = "F:\\arin.txt";

        try {

            URL website = new URL(fromFile);
            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
            FileOutputStream fos = new FileOutputStream(toFile);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
            rbc.close();

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

    }
}

References

  1. Commons IO
  2. FileChannel JavaDoc
  3. ReadableByteChannel JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Paj
6 years ago

get 403 error … how to use basic authentication to download the file?

Adrian
4 years ago

I would like to download a file from a Java server to a web page but, in the case of an error, return an error message to the web page. How can I do this? It seems that a file download can only download a file. It would be nice even to download a file and return a message with additional information. Is this possible? Many thanks in advance.

Sam
6 years ago

NIO example is bad, it might not read all the bytes. From the docs: An attempt is made to read up to count bytes from the source channel and write them to this channel’s file starting at the given position. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes will be transferred if the source channel has fewer than count bytes remaining, or if the source channel is non-blocking and has fewer than count bytes immediately available in its input buffer.

Raj
6 years ago

Getting Below error pls help me to resolve the issue

java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)

Ayush
5 years ago
Reply to  Raj

I want to download java file