Java URL Connection getBytes(String urlStr)

Here you can find the source of getBytes(String urlStr)

Description

read url into byte[]

License

Apache License

Declaration

public static byte[] getBytes(String urlStr) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /** read url into byte[] */
    public static byte[] getBytes(String urlStr) {
        try {/* ww  w  .ja  va  2 s  . c o m*/
            URL u = new URL(urlStr);
            URLConnection uc = u.openConnection();
            InputStream is = uc.getInputStream();
            byte[] buf = new byte[4096];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while (true) {
                int read = is.read(buf);
                if (read == -1) {
                    break;
                }
                baos.write(buf, 0, read);
            }
            is.close();
            baos.close();
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)
  2. getAsStream(URL url)
  3. getBaseAuthInputStreamFromURL(String query, String basicAuthString)
  4. getBaseURL(URLConnection conn)
  5. getBooleanFromUrl(String url)
  6. getCharset(URLConnection connection)
  7. getCharsFromURL(URL url)
  8. getChildren(URL url)
  9. getConnection(URL url)