Android URL Download getBytesFromUrl(String url)

Here you can find the source of getBytesFromUrl(String url)

Description

get Bytes From Url

Declaration

public static byte[] getBytesFromUrl(String url) throws Exception 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static byte[] getBytesFromUrl(String url) throws Exception {
        return readInputStream(getRequest(url));
    }/*from  ww  w.  j ava  2 s. c  o m*/

    public static byte[] readInputStream(InputStream inStream)
            throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }

    public static InputStream getRequest(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        if (conn.getResponseCode() == 200) {
            return conn.getInputStream();
        }
        return null;
    }
}

Related

  1. downloadHtmlPage(String pageUrl)
  2. downloadUrl(String urlString)
  3. getJSONResponseFromURL(String url, Hashtable httpGetParams)
  4. downloadJson(URI uri)
  5. getURLString(String urlStr)
  6. getWebContent(String pvUrl)
  7. sendPOST(final String myurl, final String paramns)
  8. connectForMultipart(String url)