StreamUtil流处理

/**
  * 通过输入流,将其转换成字节byte数组
  * 
  * @param is
  *            输入的数据流
  * @return byte[] 转化后的字节数组
  * */
 public static byte[] getByteByStream(InputStream is) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] b = new byte[1024];
  int length = 0;
  while (length != -1) {
   try {
    length = is.read(b);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if (length != -1) {
    baos.write(b, 0, length);
   }
  }
  if (is != null) {
   try {
    is.close();
    is = null;
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
  return baos.toByteArray();
 }
 /**
  * 通过URL,获取输入流InputStream
  * 
  * @param url
  *            URL地址
  * @return InputStream 获取输入流InputStream
  * */
 public static InputStream getInputStreamByUrl(String url){
  InputStream is = null;
  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpGet request = new HttpGet(url);//请求方式为get
  try {
   HttpResponse response = httpClient.execute(request);//服务端响应
   is = response.getEntity().getContent();//获取响应InputStream
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return is;
  
 }


 

你可能感兴趣的:(StreamUtil流处理)