Java URL Download downloadURL(URL url, String localPath)

Here you can find the source of downloadURL(URL url, String localPath)

Description

download URL

License

MIT License

Declaration

public static String downloadURL(URL url, String localPath) throws IOException 

Method Source Code


//package com.java2s;
/*/*from www. j  a va2  s.  c o m*/
 * Copyright 2010-2011, Sikuli.org
 * Released under the MIT License.
 *
 */

import java.net.URL;
import java.io.*;

public class Main {
    final static int DOWNLOAD_BUFFER_SIZE = 153600;

    public static String downloadURL(URL url, String localPath) throws IOException {
        InputStream reader = url.openStream();
        String[] path = url.getPath().split("/");
        String filename = path[path.length - 1];
        File fullpath = new File(localPath, filename);
        FileOutputStream writer = new FileOutputStream(fullpath);
        byte[] buffer = new byte[DOWNLOAD_BUFFER_SIZE];
        int totalBytesRead = 0;
        int bytesRead = 0;
        while ((bytesRead = reader.read(buffer)) > 0) {
            writer.write(buffer, 0, bytesRead);
            totalBytesRead += bytesRead;
        }
        reader.close();
        writer.close();

        return fullpath.getAbsolutePath();
    }
}

Related

  1. downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
  2. downloadToString(String url)
  3. downloadURL(String addr, File outFile)
  4. downloadURL(String url, String outFile)
  5. downloadURL(URL url, File file)
  6. downloadUrlToFile(String surl, File file, String method)
  7. downloadUrlToFile(URL url, File file)
  8. downloadUrlToFile(URL url, File result)
  9. downloadWebpage(String url)