来源于网络,用以备忘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static String toString(InputStream is) {
try {
ByteArrayOutputStream boa = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
boa.write(buffer, 0, len);
}
is.close();
boa.close();
byte[] result = boa.toByteArray();
String temp = new String(result);
if (temp.contains("utf-8")) {
return new String(result, "utf-8");
} else if (temp.contains("gb2312")) {
return new String(result, "gb2312");
} else {
return new String(result, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}