将输入流转换成字符串

为了方便使用,创建一个工具类,写一个静态方法
class StreamToos {

    /**
     * 将输入流流转化成字符串
     * @param is
     * @return
     */
    public static String readFromNetWork(InputStream is){

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            return baos.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_38619138/article/details/71124439