将InputStream转换为String
本文于2174天之前发表,文中内容可能已经过时。
使用Apache库
不重复造轮子。最靠谱的方法,还是用Apache commons IOUtils
这样简单几行代码就搞定了
1 | StringWriter writer = new StringWriter(); |
或者
1 | String theString = IOUtils.toString(inputStream, encoding)//这个方法其实封装了上面的方法,减少了一个参数 |
使用原生库
如果不想引入Apache库,也可以这样做
1 | static String convertStreamToString(java.io.InputStream is) { |
stackoverflow讨论地址
http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string