Java URL Download downloadToFile(String url, File file)

Here you can find the source of downloadToFile(String url, File file)

Description

Downloads from a given URL to a given File

License

Open Source License

Parameter

Parameter Description
url the URL as a String
file the File to download to

Return

true if successful

Declaration

public static boolean downloadToFile(String url, File file) 

Method Source Code

//package com.java2s;
/**/*from   w  ww .j  a v  a2s.com*/
 * CraftBoot, a bootstrapper for SKCraft Launcher
 * Copyright (C) 2014 Hayden Schiff <http://oxguy3.github.io> and contributors
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedInputStream;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Downloads from a given URL to a given File
     * 
     * @param url the URL as a String
     * @param file the File to download to
     * @return true if successful
     */
    public static boolean downloadToFile(String url, File file) {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(url).openStream());
            fout = new FileOutputStream(file);

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}

Related

  1. downloadString(URL url)
  2. downloadText(String url)
  3. downloadTextFromUrl(String url)
  4. downloadTo(String url, String filename)
  5. downloadToDirectory(URL url, String directoryName)
  6. downloadToFile(URL source, File dest)
  7. downloadToFile(URL url, File downloadFile)
  8. downloadToFile(URL url, File file)
  9. downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)