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      //缓存2KB
10         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     }

 

相关文章:

  • 2021-12-12
  • 2021-06-15
  • 2022-12-23
  • 2022-01-07
  • 2022-01-27
  • 2022-12-23
  • 2021-12-02
  • 2021-08-15
猜你喜欢
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
相关资源
相似解决方案