When getting the input stream in Java, it is sometimes necessary to flow the input into a string in order to get its contents, and summarize the way InputStream turns into string
Method 1:
Public String convertstreamtostring (InputStream is) {
BufferedReader reader = new bufferedreader (new InputStreamReader (IS));
StringBuilder sb = new StringBuilder ();
String line = null;
Try {
while (line = Reader.readline ()) = null) {
Sb.append (line + "/n");
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
Try {
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
return sb.tostring ();
}
Method 2:
Public String inputstream2string (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 ();
}
Method 3:
public static String inputstream2string (InputStream is) throws ioexception{
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
int i=-1;
while ((I=is.read ())!=-1) {
Baos.write (i);
}
return baos.tostring ();
}
InputStream String in Java