file 从InputStream读取byte[]示例

file 从InputStream读取byte[]示例

  1. public static byte[] getStreamBytes(InputStream is) throws Exception {  
  2.     ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.     byte[] buffer = new byte[1024];  
  4.     int len = 0;  
  5.     while ((len = is.read(buffer)) != -1) {  
  6.         baos.write(buffer, 0, len);  
  7.     }  
  8.     byte[] b = baos.toByteArray();  
  9.     is.close();  
  10.     baos.close();  
  11.     return b;  
  12. }  
 
  1. default byte[] readFileBytes(InputStream is){  
  2.     byte[] data = null;  
  3.     try {  
  4.         if(is.available()==0){//严谨起见,一定要加上这个判断,不要返回data[]长度为0的数组指针  
  5.             return data;  
  6.         }  
  7.         data = new byte[is.available()];  
  8.         is.read(data);  
  9.         is.close();  
  10.         return data;  
  11.     } catch (IOException e) {  
  12.         LogCore.BASE.error("readFileBytes, err", e);  
  13.         return data;  
  14.     }  

猜你喜欢

转载自www.cnblogs.com/kelelipeng/p/11910609.html