Java URL Download nio download(String link, File destFile)

Here you can find the source of download(String link, File destFile)

Description

download

License

Open Source License

Declaration

public static boolean download(String link, File destFile) 

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.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    public static boolean download(String link, File destFile) {
        return download(link, destFile, false);
    }//ww w  .  j  a v  a2s  .  com

    public static boolean download(String link, File destFile, boolean append) {
        try {
            final URL url = new URL(link);
            try (ReadableByteChannel channel = Channels.newChannel(url.openStream());
                    FileChannel fileChannel = new FileOutputStream(destFile, append).getChannel()) {
                fileChannel.transferFrom(channel, append ? (fileChannel.size()) : 0, Long.MAX_VALUE);
            }
            return true;
        } catch (IOException ex) {
            Logger.getLogger("Util").log(Level.WARNING, "Util.download", ex);
            return false;
        }
    }
}

Related

  1. canDownload(String filePath)
  2. download(File dist, URL src)
  3. download(String from, String to)
  4. download(String url, File output)
  5. download(String url, String destPath)
  6. download(String url, String inputFile, String outputPath, String outputFile)
  7. download(URI source, File target)