Java URL Download downloadUrlToFile(URL url, File result)

Here you can find the source of downloadUrlToFile(URL url, File result)

Description

download Url To File

License

BSD License

Parameter

Parameter Description
url a parameter
result a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void downloadUrlToFile(URL url, File result) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w  ww  . j a v a2s  .c o m
 * <p>
 * Utilities for manipulating Paths, Files, Directories, etc.
 * </p>
 * <p>
 * <span class="BSDLicense"> This software is distributed under the <a
 * href="http://hci.stanford.edu/research/copyright.txt">BSD License</a>.</span>
 * </p>
 * 
 * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu)
 */

import java.io.BufferedInputStream;

import java.io.DataInputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;

public class Main {
    /**
     * @param url
     * @param result
     * @throws IOException
     */
    public static void downloadUrlToFile(URL url, File result) throws IOException {
        IOException exception = null;
        InputStream is = null;
        DataInputStream dis = null;
        FileOutputStream fos = null;

        byte[] buf = new byte[1024];
        try {
            is = url.openStream();
            dis = new DataInputStream(new BufferedInputStream(is));
            fos = new FileOutputStream(result);
            int bytesRead;
            bytesRead = dis.read(buf);
            while (bytesRead > 0) {
                fos.write(buf, 0, bytesRead);
                bytesRead = dis.read(buf);
            }
        } catch (IOException ioe) {
            exception = ioe;
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException ioe) {
            }

            try {
                if (fos != null)
                    fos.close();
            } catch (IOException ioe) {
            }
            if (exception != null)
                throw exception;
        }
    }
}

Related

  1. downloadURL(String url, String outFile)
  2. downloadURL(URL url, File file)
  3. downloadURL(URL url, String localPath)
  4. downloadUrlToFile(String surl, File file, String method)
  5. downloadUrlToFile(URL url, File file)
  6. downloadWebpage(String url)
  7. downloadZip(URL file, File dump)
  8. downlod(String url, File dest)
  9. downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)