Java URL Download nio downloadFile(String url, File output)

Here you can find the source of downloadFile(String url, File output)

Description

Downloads a file from the specified URIL and saves it at the specified location.

License

Open Source License

Parameter

Parameter Description
url The URL from where to download the file from.
output The file where the file will be stored.

Exception

Parameter Description
MalformedURLException an exception
IOException an exception

Declaration

public static void downloadFile(String url, File output) throws MalformedURLException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Main {
    /**/*from  w  w  w .j  av a 2s .c  o m*/
     * Downloads a file from the specified URIL and saves it at the specified location.
     *
     * @param url The URL from where to download the file from.
     * @param output The file where the file will be stored.
     * @throws MalformedURLException
     * @throws IOException
     */
    public static void downloadFile(String url, File output) throws MalformedURLException, IOException {
        final URL website = new URL(url);
        final ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        final FileOutputStream fos = new FileOutputStream(output);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        fos.close();
    }
}

Related

  1. downloadFile(final String url, final String downloadPath)
  2. downloadFile(String fileName, String url)
  3. downloadFile(String fileURL, String folderPath)
  4. downloadFile(String inputUrl, String destination)
  5. downloadFile(String sourceUrl, File destinationFile)
  6. downloadFile(String url, String localFilePath)
  7. downloadFile(String url, String location)
  8. downloadFile(String url, String location)
  9. downloadFile(String urlPath, String local)