Java URL Download nio downloadToFile(String filename, String urlString)

Here you can find the source of downloadToFile(String filename, String urlString)

Description

Downloads data from the given URL and saves it to the given file

License

Apache License

Parameter

Parameter Description
filename - String of destination
urlString - http location of file to download

Declaration

public static void downloadToFile(String filename, String urlString) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w w w.j  av  a 2s . c  om*/
 * This file is part of FTB Launcher.
 *
 * Copyright ? 2012-2013, FTB Launcher Contributors <https://github.com/Slowpoke101/FTBLaunch/>
 * FTB Launcher is 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.File;
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 {
    /**
     * Downloads data from the given URL and saves it to the given file
     * 
     * @param filename
     *            - String of destination
     * @param urlString
     *            - http location of file to download
     */
    public static void downloadToFile(String filename, String urlString) throws IOException {
        downloadToFile(new URL(urlString), new File(filename));
    }

    /**
     * Downloads data from the given URL and saves it to the given file
     * 
     * @param url
     *            The url to download from
     * @param file
     *            The file to save to.
     */
    public static void downloadToFile(URL url, File file) throws IOException {
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(file);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        fos.close();
    }
}

Related

  1. downloadFileToDirectory(String url, File destination)
  2. downloadFirstLineFromInternetQuietly(URL url)
  3. downloadFromHttpUrl(String destPkgUrl, FileOutputStream outputStream)
  4. downloadFromInternet(URL url, File downloadTo)
  5. downloadImage(String src, Path saveFolder)
  6. downloadToFile(URL url, File file)
  7. downloadToString(String url)
  8. downloadUrl(String urlstring, File file)