private String inputStreamToString(InputStream is, String encoding) {
try {
byte[] b = new byte[1024];
String res = "";
if (is == null) {
return "";
}
int result = 0;
while (true) {
result = is.read(b, 0, 1024); // 返回读取的字节数
if (result == -1) {// 文件结束
return res;
}
res += new String(b, 0, result, encoding);
}
} catch (Exception e) {
e.printStackTrace();
System.out.print("Exception: " + e);
return "";
}
}
本文介绍了一个用于将输入流转换为字符串的方法。该方法通过循环读取输入流中的数据,并将其拼接成字符串,直到流结束。如果在处理过程中遇到异常,则会打印异常信息并返回空字符串。
959

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



