Download file from Internet in Java using apache commons-io
7 years ago Lalit Bhagtani 0
Download file from Internet in Java :-
In this tutorial, we will learn to download file from Internet in java using apache’s commons-io library. Let’s see an implementation using commons-io library with the help of an example :-
Apache Commons-IO :-
In case of commons-io library, we will call copyURLToFile() method of FileUtils class by passing URL and File Object as a parameter. If you don’t have this library in your local setup, then you can download it by using maven or gradle configuration.
Maven :-
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
Gradle :-
dependencies { compile 'commons-io:commons-io:2.5' }
Example :-
import java.io.File; import java.io.IOException; import java.net.URL; import org.apache.commons.io.FileUtils; public class DownloadFileFromURL { public static void main(String[] args){ String url = "https://codedestine.com/text.html"; String file = "D://file.txt"; try { //connectionTimeout, readTimeout = 10 seconds FileUtils.copyURLToFile(new URL(url), new File(file), 10000, 10000); }catch (IOException e) { e.printStackTrace(); } } }
You may also like :-
References :-
That’s all for Download file from Internet in Java, If you liked it, please share your thoughts in comments section and share it with others too.