Java URL Load saveURL(URL url, OutputStream os)

Here you can find the source of saveURL(URL url, OutputStream os)

Description

Opens a buffered stream on the url and copies the contents to OutputStream

License

Open Source License

Declaration

public static void saveURL(URL url, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
import java.net.*;
import java.io.*;

public class Main {
    /**//  www  .  j  a v  a2s  .co m
     * Opens a buffered stream on the url and copies the contents to writer
     */
    public static void saveURL(URL url, Writer writer) throws IOException {
        BufferedInputStream in = new BufferedInputStream(url.openStream());
        for (int c = in.read(); c != -1; c = in.read()) {
            writer.write(c);
        }
    }

    /**
     * Opens a buffered stream on the url and copies the contents to OutputStream
     */
    public static void saveURL(URL url, OutputStream os) throws IOException {
        InputStream is = url.openStream();
        byte[] buf = new byte[1048576];
        int n = is.read(buf);
        while (n != -1) {
            os.write(buf, 0, n);
            n = is.read(buf);
        }
    }
}

Related

  1. saveFileFromURL(URL url, File destinationFile)
  2. saveImage(String imageUrl, String destinationFile)
  3. saveImageAsFile(String imageUrl, String destinationFile, boolean overwrite)
  4. saveUrl(String filename, String urlString)
  5. saveUrl(String filename, URL url)