从一个URL下载原始数据,基于byte字节,得到byte数组

来源:互联网 发布:mac用 电子书app txt 编辑:程序博客网 时间:2024/06/07 23:17
public static byte[] loadRawDataFromURL(String u) throws Exception {        URL url = new URL(u);        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        InputStream is = conn.getInputStream();        BufferedInputStream bis = new BufferedInputStream(is);        ByteArrayOutputStream baos = new ByteArrayOutputStream();     //缓存2KB        final int BUFFER_SIZE = 2*1024;        final int EOF = -1;        int c;        byte[] buf = new byte[BUFFER_SIZE];        while (true) {            c = bis.read(buf);            if (c == EOF)                break;            baos.write(buf, 0, c);        }        conn.disconnect();        is.close();        byte[] data = baos.toByteArray();        baos.flush();        return data;    }

0 0