Java URL Download nio download(URL url, File out)

Here you can find the source of download(URL url, File out)

Description

Copies the content of a URL to a local file.

License

Apache License

Parameter

Parameter Description
url source url
out file to write content to

Declaration

public static void download(URL url, File out) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Main {
    /**//w w w.  j av a2s .c  o m
     * Copies the content of a URL to a local file.
     *
     * @param url source url
     * @param out file to write content to
     */
    public static void download(URL url, File out) throws IOException {
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(out);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        fos.close();
        rbc.close();
    }

    /**
     * Copies the content of a URL to a local file.
     *
     * @param url source url
     * @param out file to write content to
     */
    public static void download(String url, File out) throws IOException {
        URL u = new URL(url);
        download(u, out);
    }
}

Related

  1. download(String url, String destPath)
  2. download(String url, String inputFile, String outputPath, String outputFile)
  3. download(URI source, File target)
  4. download(URL link, File outputFile)
  5. download(URL url, File out)
  6. download(URL url, Path location)
  7. download(URL url, String save)
  8. downloadAsync(String url, File file)
  9. downloadFile(final String url, final String downloadPath)