将输入流InputStream转换为String
程序员文章站
2024-02-04 08:08:58
...
InputStream转换为String
/**
* @author Administrator
* @param InputStream
* InputStream to String
*/
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
也可以使用apache的io包 用来转换InputStream到String,代码示例如下
import org.apache.commons.io.IOUtils;
public static String InputStreamToString(InputStream in){
String str = null;
try {
str = IOUtils.toString(in, "UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
上一篇: 重复读取inputStream