Java URL Load saveUrl(String filename, URL url)

Here you can find the source of saveUrl(String filename, URL url)

Description

save Url

License

Open Source License

Declaration

public static void saveUrl(String filename, URL url) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  ww.  java  2s . co  m
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Cayde Dixon
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

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

public class Main {
    public static void saveUrl(String filename, URL url) throws IOException {
        //noinspection ResultOfMethodCallIgnored
        new File(filename).getParentFile().mkdirs();
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(url.openStream());
            fout = new FileOutputStream(filename);
            copyStream(in, fout);
        } finally {
            if (in != null)
                in.close();
            if (fout != null)
                fout.close();
        }
    }

    public static void copyStream(InputStream input, OutputStream output) {
        try {
            final byte data[] = new byte[8192];
            int count;
            while ((count = input.read(data, 0, 8192)) != -1)
                output.write(data, 0, count);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. saveFile(final URL url, final File file)
  2. saveFileFromURL(URL url, File destinationFile)
  3. saveImage(String imageUrl, String destinationFile)
  4. saveImageAsFile(String imageUrl, String destinationFile, boolean overwrite)
  5. saveUrl(String filename, String urlString)
  6. saveURL(URL url, OutputStream os)