How do I read / convert an InputStream into a String in Java?(如何在 Java 中将 InputStream 读取/转换为字符串?)
问题描述
If you have a java.io.InputStream
object, how should you process that object and produce a String
?
Suppose I have an InputStream
that contains text data, and I want to convert it to a String
, so for example I can write that to a log file.
What is the easiest way to take the InputStream
and convert it to a String
?
A nice way to do this is using Apache commons IOUtils
to copy the InputStream
into a StringWriter
... something like
or even
Alternatively, you could use ByteArrayOutputStream
if you don't want to mix your Streams and Writers
这篇关于如何在 Java 中将 InputStream 读取/转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!