将InputStream转换成String字符串
public class StreamTools {
/**
* @param is 输入流
* @return String 返回的字符串
* @throws IOException
*/
public static String readFromStream(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
String result = baos.toString();
baos.close();
return result;
}
}
InputStream转String方法
本文介绍了一个实用的方法,用于将Java中的InputStream对象转换为String类型。通过使用ByteArrayOutputStream和缓冲区,该方法能够有效地读取输入流中的所有数据,并将其转换为字符串形式。
867

被折叠的 条评论
为什么被折叠?



