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

小记:加载网络图片返回字节数组。

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

 




原标题:小记:加载网络图片返回字节数组。

关键词:网络

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

极兔的另一盘棋?Jet Commerce的东南亚电商征途:https://www.goluckyvip.com/news/5935.html
现场直击!CJ物流今起正式罢工,韩国年末配送......:https://www.goluckyvip.com/news/5936.html
Lazada上线LazPick精选商家计划,普通商家的救星来了!:https://www.goluckyvip.com/news/5937.html
中国海关总署:RCEP实施在即 海关准备就绪:https://www.goluckyvip.com/news/5938.html
英国Tiktok shop开通!怎么找出tiktok带货账号:https://www.goluckyvip.com/news/5939.html
Wish Express现已支持巴西路向配送:https://www.goluckyvip.com/news/594.html
青岛崂山旅游攻略自驾游(详细介绍崂山自驾游路线和景点):https://www.vstour.cn/a/404242.html
海南旅游景点的路线 海南旅游景点大全攻略自驾游:https://www.vstour.cn/a/404243.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流