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;
}
}
网友评论