首页 > 技术文章 > 数据流InputStream转字符串

gwd1154978352 2017-05-07 20:59 原文


    public static String ScreamToString(InputStream in) throws Exception{
     //定义一个内存输出流
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     int len=0;
     byte[] bt=new byte[1024];
     while((len=in.read(bt))!=-1){
        out.write(bt,0,len);
     }
     String content=new String(out.toByteArray());//使用构造函数的原因是
                                            //还可以通过new String(out.toByteArray(),"utf-8")来设置编码
     return content;
    }

推荐阅读