你的位置:首页 > 软件开发 > 操作系统 > 从一个URL下载原始数据,基于byte字节,得到byte数组

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

发布时间:2015-11-25 11:00:53
1 public static byte[] loadRawDataFromURL(String u) throws Exception { 2 URL url = new URL(u); 3 HttpURLConnection conn = (HttpURLCo ...
 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   }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

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

关键词:URL

URL
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。