Talk is cheap, Show me the code. -- by: Linus Torvalds
方式一、
JAVA8+ Stream API
String result = new BufferedReader(new InputStreamReader(inputStream))
.lines() // 按行读取
.collect(Collectors.joining("n")); // 合并行为字符串
方式二、
使用commons-io
String result = IOUtils.toString(inputStream, "UTF-8");
方式三、
使用Guava
String result = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
方式四、
使用ByteArrayOutputStream
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();}return buf.toString("UTF-8");