Java URL Download nio download(URL link, File outputFile)

Here you can find the source of download(URL link, File outputFile)

Description

Download a file from a specific url.

License

Open Source License

Parameter

Parameter Description
link the link
outputFile the output file

Exception

Parameter Description
Exception the exception

Declaration

public static void download(URL link, File outputFile) throws Exception 

Method Source Code

//package com.java2s;
/**//from w  ww  . j a v  a  2 s  . co m
 * SurvivalPlus 1.0
 * Copyright (C) 2014 CodingBadgers <plugins@mcbadgercraft.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Download a file from a specific url.
     *
     * @param link       the link
     * @param outputFile the output file
     * @throws Exception the exception
     */
    public static void download(URL link, File outputFile) throws Exception {
        if (link == null)
            throw new IllegalArgumentException("dl link cannot be null");

        ReadableByteChannel rbc = Channels.newChannel(link.openStream());
        FileOutputStream output = new FileOutputStream(outputFile);
        output.getChannel().transferFrom(rbc, 0, 1 << 24);
        output.close();
    }
}

Related

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