//不带编码的,遇到中文乱码就挂
public static String inputStream2String(InputStream is) throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
//带编码的
public static String inputStream2String (InputStream in , String encoding) throws Exception {
StringBuffer out = new StringBuffer();
InputStreamReader inread = new InputStreamReader(in,encoding);
char[] b = new char[4096];
for (int n; (n = inread.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
//带编码的
public static String inputStream2String (InputStream in , String encoding) throws Exception {
StringBuffer out = new StringBuffer();
InputStreamReader inread = new InputStreamReader(in,encoding);
char[] b = new char[4096];
for (int n; (n = inread.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/283255,如需转载请自行联系原作者