Java URL Download download(String urlString, File output)

Here you can find the source of download(String urlString, File output)

Description

Download a file from a url to the local file system

License

Apache License

Parameter

Parameter Description
urlString a parameter
output a parameter

Declaration

public static File download(String urlString, File output) 

Method Source Code

//package com.java2s;
/**************************************************************
 * /*ww w .  j av a 2 s  . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.io.OutputStream;

import java.net.URI;

import java.net.URLConnection;

public class Main {
    /**
     * Download a file from a url to the local file system
     * 
     * @param urlString
     * @param output
     * @return
     */
    public static File download(String urlString, File output) {
        return download(urlString, output, false);
    }

    public static File download(String urlString, File output, boolean usetimestamp) {
        return download(urlString, output, false, null);
    }

    /**
     * Download a file from a url to the local file system
     * 
     * @param urlString
     * @param output
     * @param usetimestamp
     * @return
     */
    public static File download(String urlString, File output, boolean usetimestamp, boolean[] skip) {
        InputStream in = null;
        OutputStream out = null;
        try {
            URI url = new URI(urlString);
            URLConnection urlConnection = url.toURL().openConnection();
            int totalSize = urlConnection.getContentLength();
            in = urlConnection.getInputStream();
            if (output.isDirectory())
                output = new File(output, new File(url.getPath()).getName());
            output.getParentFile().mkdirs();
            if (usetimestamp && output.exists()) {
                if (output.lastModified() == urlConnection.getLastModified()) {
                    // log.info(MessageFormat.format(" Skip! Download {0} -> {1}",
                    // urlString, output));
                    if (skip != null && skip.length > 0)
                        skip[0] = true;
                    return output;
                }
            }

            out = new FileOutputStream(output);
            byte[] buffer = new byte[1024 * 100]; // 100k
            int count = 0;
            int totalCount = 0;
            int progress = 0;
            while ((count = in.read(buffer)) > 0) {
                out.write(buffer, 0, count);
                totalCount += count;

                if (totalSize > 0) {
                    int nowProgress = totalCount * 10 / totalSize;
                    if (nowProgress > progress) {
                        progress = nowProgress;
                    }
                }

            }
            out.close();
            if (urlConnection.getLastModified() >= 0)
                output.setLastModified(urlConnection.getLastModified());
            // log.info(MessageFormat.format("OK! Download {0} -> {1}",
            // urlString, output));
            if (skip != null && skip.length > 0)
                skip[0] = false;
            return output;
        } catch (Exception e) {
            // log.log(Level.SEVERE,
            // MessageFormat.format("Fail! Download {0} -> {1}", urlString,
            // output), e);
            return null;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                }
            if (out != null)
                try {
                    out.close();
                } catch (IOException e) {

                }
        }
    }
}

Related

  1. download(String url, String path)
  2. download(String url, String saveFile)
  3. download(String url, String toFile)
  4. download(String urlAddress, File file)
  5. download(String urlString)
  6. download(URL source, File destination)
  7. download(URL sourceUrl, File destinationFile)
  8. download(URL url)
  9. download(URL url, File destination)