【HttpUrlConnection】自定义StreamToString--流转换成字符串(1.1)

来源:互联网 发布:mac电脑剪切快捷键 编辑:程序博客网 时间:2024/06/07 18:04




  1. public class StringUtils {
  2. public static String StreamToString(InputStream in) throws IOException{
  3. //定义一个内存输出流
  4. ByteArrayOutputStream baos=new ByteArrayOutputStream();
  5. //读取流
  6. byte[] buf = new byte[1024];
  7. int len=-1;
  8. while((len=in.read(buf))!=-1){
  9. baos.write(buf,0,len);}
  10. //将流转换成字符串
  11. String content = new String(baos.toString());
  12. return content;
  13. }
  14. }


0 0