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

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

Description

download

License

Open Source License

Declaration

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

Method Source Code

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

import java.io.*;

import java.net.*;
import java.nio.channels.Channels;

import java.nio.channels.ReadableByteChannel;

import java.nio.file.*;

import java.util.*;

public class Main {
    public static void download(URL url, File out) throws IOException {
        File parent = out.getParentFile();
        if (!out.exists()) {
            if (parent != null)
                parent.mkdirs();/*from  w w  w .  ja  va 2 s  . c o  m*/
            File tempFile = File.createTempFile(UUID.randomUUID()
                    .toString(), ".tmp", parent);
            tempFile.deleteOnExit();
            try (InputStream is = url.openStream()) {
                ReadableByteChannel rbc = Channels.newChannel(is);
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
            Files.copy(tempFile.toPath(), out.toPath(),
                    StandardCopyOption.REPLACE_EXISTING);
            tempFile.delete();
        }
    }
}

Related

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