流转换成字符,流转换成字节流。stream2String,stream2Bytes

来源:互联网 发布:群发短信软件哪个好 编辑:程序博客网 时间:2024/05/17 01:32


public class StreamUtil {
public static String streamToString(InputStream inputStream)
throws Exception {
StringBuffer result = new StringBuffer();
// InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
while ((inputLine = buffer.readLine()) != null) {
result.append(inputLine);
}
return result.toString();
}


public static byte[] streamToByte(InputStream in) throws Exception {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((len = in.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
in.close();
return outputStream.toByteArray();
}
}
0 0
原创粉丝点击