星空网 > 软件开发 > 操作系统

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

 1 public static byte[] loadRawDataFromURL(String u) throws Exception { 2     URL url = new URL(u); 3     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 4  5     InputStream is = conn.getInputStream(); 6     BufferedInputStream bis = new BufferedInputStream(is); 7  8     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 9      //缓存2KB10     final int BUFFER_SIZE = 2*1024;11     final int EOF = -1;12 13     int c;14     byte[] buf = new byte[BUFFER_SIZE];15 16     while (true) {17       c = bis.read(buf);18       if (c == EOF)19         break;20 21       baos.write(buf, 0, c);22     }23 24     conn.disconnect();25     is.close();26 27     byte[] data = baos.toByteArray();28     baos.flush();29 30     return data;31   }

 




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

关键词:URL

URL
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流