Java URL Download downloadFile(File target, String urlStr)

Here you can find the source of downloadFile(File target, String urlStr)

Description

download File

License

Open Source License

Declaration

public static boolean downloadFile(File target, String urlStr) 

Method Source Code


//package com.java2s;
/*//from   w  ww.  ja  va  2s. c  o m
 * Xapp (pronounced Zap!), A automatic gui tool for Java.
 * Copyright (C) 2009 David Webber. All Rights Reserved.
 *
 * The contents of this file may be used under the terms of the GNU Lesser
 * General Public License Version 2.1 or later.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

import java.io.*;

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

public class Main {
    public static boolean downloadFile(File target, String urlStr) {
        InputStream in = openStream(urlStr);
        if (in != null) {
            System.out.println("downloading " + urlStr + "...");
            writeStreamToFile(target, in);
            System.out.println("   ...done");
        } else {
            System.out.println("no file found at " + urlStr);
        }
        return in != null;
    }

    public static InputStream openStream(String urlStr) {
        URL url;
        try {
            url = new URL(urlStr);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        InputStream in;
        try {
            in = url.openStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return in;
    }

    /**
     * First reads in all bytes fromt the stream into a byte buffer and then flushes them to file
     *
     * @param target
     * @param in
     */
    private static void writeStreamToFile(File target, InputStream in) {
        try {
            byte[] byteArray = streamToByteArray(in);
            FileOutputStream fileOutputStream = new FileOutputStream(target);
            fileOutputStream.write(byteArray);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static byte[] streamToByteArray(InputStream in) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i;
            while ((i = in.read()) != -1) {
                baos.write(i);
            }
            byte[] byteArray = baos.toByteArray();
            return byteArray;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. downloadAsString(String url)
  2. downloadBinary(URL BaseURL, String Name, File TargetDirectory)
  3. downloadData(String url, String params)
  4. downloadDirectory(URL dirUrl, File destDir)
  5. downloadFile(File parent, String prefix, String suffix, URL url)
  6. downloadFile(final String url)
  7. downloadFile(String fileURL, String localFileName, String destinationDir)
  8. downloadFile(String fileURL, String saveDir)
  9. downloadFile(String fileURL, String savePath)