zoukankan      html  css  js  c++  java
  • 小记:加载网络图片返回字节数组。

     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         // 缓存2KB
    18         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 }
  • 相关阅读:
    EntityFramework系列:MySql的RowVersion
    EntityFramework系列:SQLite.CodeFirst自动生成数据库
    怎么回事呢?
    为蛇么不现实
    发布到个人主页
    作别
    budao 首页
    中午吃饱了
    作业写好了吗?
    分类
  • 原文地址:https://www.cnblogs.com/labixiaoxin/p/5051443.html
Copyright © 2011-2022 走看看