Java URL Download nio downloadFile(String inputUrl, String destination)

Here you can find the source of downloadFile(String inputUrl, String destination)

Description

download File

License

Apache License

Declaration

public synchronized static Path downloadFile(String inputUrl, String destination) 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public synchronized static Path downloadFile(String inputUrl, String destination) {
        String filename = Paths.get(inputUrl).getFileName().toString();

        URL url;/*w  w w.ja  va  2  s  .  c om*/
        try {
            url = new URL(inputUrl);
        } catch (MalformedURLException e) {
            return null;
        }

        Path path;
        URLConnection con;
        try {
            con = url.openConnection();
            path = Paths.get(destination + File.separator + filename);

            try (InputStream stream = con.getInputStream()) {
                Files.copy(stream, path);
            }

            return path;
        } catch (IOException e) {
            return null;
        }
    }
}

Related

  1. download(URL url, String save)
  2. downloadAsync(String url, File file)
  3. downloadFile(final String url, final String downloadPath)
  4. downloadFile(String fileName, String url)
  5. downloadFile(String fileURL, String folderPath)
  6. downloadFile(String sourceUrl, File destinationFile)
  7. downloadFile(String url, File output)
  8. downloadFile(String url, String localFilePath)
  9. downloadFile(String url, String location)