Java URL Download download(URL url)

Here you can find the source of download(URL url)

Description

download

License

Open Source License

Declaration

public static byte[] download(URL url) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static byte[] download(URL url) throws IOException {
        return download(url.toString(), 60000, 60000);
    }/*from   w w  w  .j a v a 2  s.c  o  m*/

    public static byte[] download(String url, int connectTimeout, int readTimeout) throws IOException {
        URLConnection con = new URL(url).openConnection();
        con.setConnectTimeout(connectTimeout);
        con.setReadTimeout(readTimeout);
        InputStream in = con.getInputStream();
        byte[] content;
        try {
            content = toBytes(in);
        } finally {
            in.close();
        }
        return content;
    }

    private static byte[] toBytes(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copy(in, out);
        return out.toByteArray();
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buff = new byte[256];
        int len = in.read(buff);
        while (len != -1) {
            out.write(buff, 0, len);
            len = in.read(buff);
        }
    }
}

Related

  1. download(String urlAddress, File file)
  2. download(String urlString)
  3. download(String urlString, File output)
  4. download(URL source, File destination)
  5. download(URL sourceUrl, File destinationFile)
  6. download(URL url, File destination)
  7. download(URL url, File file)
  8. downloadAndExtract(String fileURL, String targetDirectory)
  9. downloadAndSaveImage(URL imageUrl, String path)