Java URL Download download(URL sourceUrl, File destinationFile)

Here you can find the source of download(URL sourceUrl, File destinationFile)

Description

This method given a URL sourceURL and file path destinationFile writes the contents of the URL to file.

License

Apache License

Parameter

Parameter Description
sourceUrl Source URL
destinationFile Destination file

Exception

Parameter Description
IOException an exception

Declaration

public static void download(URL sourceUrl, File destinationFile) throws IOException 

Method Source Code

//package com.java2s;
/*// ww  w .ja  v a 2 s .  com
 * Copyright 2009 the original author or authors.
 * Copyright 2009 SorcerSoft.org.
 *  
 * 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.io.InputStream;

import java.net.URL;

public class Main {
    /**
     * This method given a URL sourceURL and file path destinationFile writes
     * the contents of the URL to file.
     * 
     * @param sourceUrl
     *            Source URL
     * @param destinationFile
     *            Destination file
     * @see writeUrlToFile
     * @throws IOException
     */
    public static void download(URL sourceUrl, File destinationFile) throws IOException {
        writeUrlToFile(sourceUrl, destinationFile);
    }

    /**
     * Method that writes the content of URL inputURL to file path
     * locatlInputFile
     * 
     * @param inputUrl
     *            Input URL
     * @param localInputFile
     *            File path
     * @throws IOException
     */
    public static void writeUrlToFile(URL inputUrl, File localInputFile) throws IOException {
        InputStream is = inputUrl.openStream();
        redirectInputStream2File(is, localInputFile);
    }

    /**
     * This method redirects an input stream to a file
     * 
     * @param is Input stream
     * @param file File
     * @throws IOException
     */
    public static void redirectInputStream2File(InputStream is, File file) throws IOException {
        int bufferSize = 4096;
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[bufferSize];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) { // EOF
            fos.write(buffer, 0, bytesRead);
        }
        fos.close();
        is.close();
    }
}

Related

  1. download(String url, String toFile)
  2. download(String urlAddress, File file)
  3. download(String urlString)
  4. download(String urlString, File output)
  5. download(URL source, File destination)
  6. download(URL url)
  7. download(URL url, File destination)
  8. download(URL url, File file)
  9. downloadAndExtract(String fileURL, String targetDirectory)