Tutorial

Java Download File from URL

Published on August 3, 2022
Default avatar

By Pankaj

Java Download File from URL

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.

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.

Java Download File from URL

java download file from url, java code to download file from URL example 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.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 23, 2020

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

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 8, 2020

    Any file will be downloaded from this code, like JPEG, PDF, CSV ???

    - Jagdeep

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      February 22, 2019

      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

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        July 26, 2016

        Thanks. I used this code in Android and it is working fine.

        - Nuwanga G.

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          June 4, 2015

          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

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 15, 2014

            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

              Try DigitalOcean for free

              Click below to sign up and get $200 of credit to try our products over 60 days!

              Sign up

              Join the Tech Talk
              Success! Thank you! Please check your email for further details.

              Please complete your information!

              Get our biweekly newsletter

              Sign up for Infrastructure as a Newsletter.

              Hollie's Hub for Good

              Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

              Become a contributor

              Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

              Welcome to the developer cloud

              DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

              Learn more
              DigitalOcean Cloud Control Panel