package com.example.administrator.mobilesafe;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Administrator on 2017/8/29.
*/
public class StreamUtil {
public static String streamToString(InputStream inputStream) {
// 1 思路在读取过程中将读取的内容存储到缓存中 然后一次转化为字符串返回
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 2 流操作读到没有为止(循环)
byte[] buffer = new byte[1024];
// 3 记录读取内容的临时变量
int temp = -1;
try {
// 网络上的输入流 读入缓冲区
while ((len = in.read(buff)) != -1) {
// 从缓冲区写入内存
bos.write(buff, 0, len);
}
return byteArrayOutputStream.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
byteArrayOutputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
streamToStringUtils
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。