您的位置:首页 > 其它

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

2015-11-25 10:07 387 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: