「Java入門」InputStreamをStringに変換する方法まとめ
方法1
public String inputStreamToString (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
方法2
public static String inputStreamToString(InputStream is) throws IOException{
ByteArrayOutputStream ctnInfo = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
ctnInfo.write(i);
}
return ctnInfo.toString();
}