InputStream2String
程序员文章站
2024-02-04 08:22:40
...
public static String InputStream2String(InputStream in) {
return InputStream2String(in, null);
}
public static String InputStream2String(InputStream in,String charset) {
if (charset == null){
charset = "UTF-8";
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String result = null;
int bufferSize = 2048;
byte[] data = new byte[bufferSize];
int count = -1;
try {
while ((count = in.read(data, 0, bufferSize)) != -1) {
outputStream.write(data, 0, count);
}
result = new String(outputStream.toByteArray(), charset);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
推荐阅读