Java Download File from URL

Filed Under: Java

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.

Comments

  1. mahesh says:

    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/%5EBSESN?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)

    1. Pankaj says:

      The server might be blocking it, can you try after setting the User-Agent header? That way, it will look like the request is coming from a browser.

  2. Nuwanga G. says:

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

  3. Ravi says:

    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)

  4. Gaurab Pradhan says:

    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

    1. Tran Tuan says:

      I don’t understand why my program not run
      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.FileReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.URL;
      import java.net.URLConnection;

      import java.net.*;
      import java.io.*;

      public class MainClass {
      public static void main(String[] args) throws IOException {
      String strData = “javascript:playMp3(‘http://www.englishspeak.com/instantspeak/English/phrases/mp3/aFew.mp3’)”;
      String strFind = “http://www.englishspeak.com/instantspeak/English/phrases/mp3/”;

      File file = new File(“D:\\htmlformp3.txt”);
      FileReader fr = new FileReader(file);
      BufferedReader br = new BufferedReader(fr);
      String line;
      while ((line = br.readLine()) != null) {
      String result = getSubString(line, strFind);
      if(result != null){
      System.out.println(result);
      }
      }
      br.close();
      fr.close();
      }

      public static String getSubString(String strParent, String strSub){
      int index = strParent.indexOf(strSub);
      if(index >= 0){
      int subIndex = index + strSub.length();

      for(int i = subIndex; i < strParent.length(); i++){
      if ('.' == strParent.charAt(i)){
      break;
      } else {
      strSub += strParent.charAt(i);
      }
      }
      return strSub + ".mp3";
      }
      return null;
      }
      }

      Please review help me
      Thanks

      1. Tran Tuan says:

        Oh sorry, cause my eclipse
        It’s run normal
        hehe, sorry about inconvenient

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages