Java URL Download nio downloadFile(final String url, final String downloadPath)

Here you can find the source of downloadFile(final String url, final String downloadPath)

Description

Download file.

License

Apache License

Parameter

Parameter Description
url the url
downloadPath the download path

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

public static void downloadFile(final String url, final String downloadPath) throws IOException 

Method Source Code


//package com.java2s;
/*// w  ww .  j a v a  2  s.  co  m
 * Copyright (c) 2015 Alexandre Almeida.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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 {
    /**
     * Download file.
     *
     * @param url
     *            the url
     * @param downloadPath
     *            the download path
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void downloadFile(final String url, final String downloadPath) throws IOException {
        URL website = new URL(url);
        try (ReadableByteChannel rbc = Channels.newChannel(website.openStream())) {
            try (FileOutputStream fos = new FileOutputStream(downloadPath)) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
        }
    }
}

Related

  1. download(URL url, File out)
  2. download(URL url, File out)
  3. download(URL url, Path location)
  4. download(URL url, String save)
  5. downloadAsync(String url, File file)
  6. downloadFile(String fileName, String url)
  7. downloadFile(String fileURL, String folderPath)
  8. downloadFile(String inputUrl, String destination)
  9. downloadFile(String sourceUrl, File destinationFile)