关于inputStream 对象重复使用的解决方法

来源:互联网 发布:莫言哪本书最好看 知乎 编辑:程序博客网 时间:2024/05/16 10:29
/**     * 读取输入流数据     * //此方法是用于缓存H5网络请求数据,解决inputStream对象不能重复复用的问题     */    public static byte[] streamToData(InputStream uristream) {        ByteArrayOutputStream outStream = null;        try {            outStream = new ByteArrayOutputStream();            byte[] buffer = new byte[1024];            int len = 0;            while ((len = uristream.read(buffer)) != -1) {                outStream.write(buffer, 0, len);            }            return outStream.toByteArray();        } catch (Exception e) {            return null;        } finally {            try {                if(uristream !=null) {                    uristream.close();                }                if (outStream != null) {                    outStream.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }
byte[] data = FileUtils.streamToData(inputstream);//把需要复用的inputStream保存为data
InputStream in1 = new ByteArrayInputStream(data)InputStream in2 = new ByteArrayInputStream(data)






0 0
原创粉丝点击