Today we will learn how to download a file from URL in java. We can use java.net.URL
openStream()
method to download file from URL in java program. We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.
Here is the simple java download file from URL example program. It shows both ways to download file from URL in java. JavaDownloadFileFromURL.java
package com.journaldev.files;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class JavaDownloadFileFromURL {
public static void main(String[] args) {
String url = "https://www.journaldev.com/sitemap.xml";
try {
downloadUsingNIO(url, "/Users/pankaj/sitemap.xml");
downloadUsingStream(url, "/Users/pankaj/sitemap_stream.xml");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void downloadUsingStream(String urlStr, String file) throws IOException{
URL url = new URL(urlStr);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fis = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int count=0;
while((count = bis.read(buffer,0,1024)) != -1)
{
fis.write(buffer, 0, count);
}
fis.close();
bis.close();
}
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
downloadUsingStream: In this method of java download file from URL, we are using URL openStream
method to create the input stream. Then we are using a file output stream to read data from the input stream and write to the file. downloadUsingNIO: In this download file from URL method, we are creating byte channel from URL stream data. Then use the file output stream to write it to file. You can use any of these methods to download the file from URL in java program. If you are looking for performance, then do some analysis by using both methods and see what suits your need.
You can checkout more Java IO examples from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Thanks Pankaj for a very good example, explained is few simple steps. However, since I am a beginner in the field, I still cant apply the example to real exercise, I am trying to solve. The following is the exercise: Download the Document “1k_most_common.txt” from the following URL & save the file: • https://github.com/DavidWittman/wpxmlrpcbrute/blob/master/wordlists/1000-most-common-passwords.txt. Later you will need to open the file & edit the content. Please, I need your assistance as soon as you can. Have tried with other few examples but cant just complete it. I will appreciate. Thanks.
- Henzhi Mkali
Any file will be downloaded from this code, like JPEG, PDF, CSV ???
- Jagdeep
run: Hello sir, Actually I want to download .csv file from yahoo finance website with the help of URL. I went through this code with different URL but it throws the following exception could you please help me out with this problem. java.io.IOException: Server returned HTTP response code: 401 for URL: https://query1.finance.yahoo.com/v7/finance/download/^BSESN?period1=1392921000&period2=1550687400&interval=1d&events=history&crumb=zmavVqRmDj/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263) at java.net.URL.openStream(URL.java:1045) at testjava.JavaDownloadFileFromURL.downloadUsingNIO(JavaDownloadFileFromURL.java:49) at testjava.JavaDownloadFileFromURL.main(JavaDownloadFileFromURL.java:25)
- mahesh
Thanks. I used this code in Android and it is working fine.
- Nuwanga G.
If I execute the same example I am getting below exception java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at sun.net.NetworkClient.doConnect(NetworkClient.java:163) at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) at sun.net.www.http.HttpClient.(HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:975) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:841) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177) at java.net.URL.openStream(URL.java:1010) at com.snp.beans.DownloadFileFromURL.downloadUsingNIO(DownloadFileFromURL.java:39) at com.snp.beans.DownloadFileFromURL.main(DownloadFileFromURL.java:16)
- Ravi
Can you please tell me how to download file from dynamic URL
www.bidsync.com/bidsync-app-web/vendor/links/bid_detail/BidDocumentsDownload.xhtml?auctionId=1952491&documentIds=5793068&contentDisposition=inline
- Gaurab Pradhan