Here you can find the source of downloadFile(final String url, final String downloadPath)
Parameter | Description |
---|---|
url | the url |
downloadPath | the download path |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static void downloadFile(final String url, final String downloadPath) throws IOException
//package com.java2s; /*// w ww . j a v a 2 s. co m * Copyright (c) 2015 Alexandre Almeida. * * Licensed 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.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; public class Main { /** * Download file. * * @param url * the url * @param downloadPath * the download path * @throws IOException * Signals that an I/O exception has occurred. */ public static void downloadFile(final String url, final String downloadPath) throws IOException { URL website = new URL(url); try (ReadableByteChannel rbc = Channels.newChannel(website.openStream())) { try (FileOutputStream fos = new FileOutputStream(downloadPath)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } } } }