Here you can find the source of downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
Parameter | Description |
---|---|
targetFile | a parameter |
requestedURL | a parameter |
proxy | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
FileNotFoundException | an exception |
private static void downloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy) throws IOException, FileNotFoundException
//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. } } } }