Java URL Download downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)

Here you can find the source of downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)

Description

download To Local File

License

Open Source License

Parameter

Parameter Description
targetFile a parameter
requestedURL a parameter
proxy a parameter

Exception

Parameter Description
IOException an exception
FileNotFoundException an exception

Declaration

private static void downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
        throws IOException, FileNotFoundException 

Method Source Code


//package com.java2s;
/*//ww  w.j a v  a2 s . c  o  m
 org.org.lib.maven2 is a java library/OSGI Bundle
 Providing Maven repository related functionalities.
 Copyright (C) 2007  Pierre-Antoine Gr?goire
     
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
     
 This library 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
 Lesser General Public License for more details.
     
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

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

import java.net.Proxy;
import java.net.URL;

public class Main {
    /**
     * @param targetFile
     * @param requestedURL
     * @param proxy
     * @throws IOException
     * @throws FileNotFoundException
     */
    private static void downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
            throws IOException, FileNotFoundException {
        InputStream is = open(requestedURL, proxy);
        targetFile.createNewFile();
        FileOutputStream out = new FileOutputStream(targetFile);
        byte[] buf = new byte[1024]; // 1K buffer
        int bytesRead;
        while ((bytesRead = is.read(buf)) != -1) {
            out.write(buf, 0, bytesRead);
        }
        close(is);
        out.close();
    }

    private static InputStream open(URL url, Proxy proxy) throws IOException {
        InputStream inputStream = null;
        if (proxy != null) {
            inputStream = url.openConnection(proxy).getInputStream();
        } else {
            inputStream = url.openConnection().getInputStream();
        }
        return inputStream;
    }

    private static void close(InputStream in) {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e1) {
                // ignore.
            }
        }
    }
}

Related

  1. downloadToDirectory(URL url, String directoryName)
  2. downloadToFile(String url, File file)
  3. downloadToFile(URL source, File dest)
  4. downloadToFile(URL url, File downloadFile)
  5. downloadToFile(URL url, File file)
  6. downloadToString(String url)
  7. downloadURL(String addr, File outFile)
  8. downloadURL(String url, String outFile)
  9. downloadURL(URL url, File file)