Source:
CSDN: InputStream类available和read方法可能读取不到完整的流数据
stackoverflow: How to read a http file inmemory without saving on local drive?
1 2 3 4 5 6 7 8 9 10 11 |
private static InputStream download(String sourceURL) throws Exception { InputStream inputStream = new URL(sourceURL).openStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = inputStream.read(buffer))) { output.write(buffer, 0, n); } return new ByteArrayInputStream(output.toByteArray()); } |