String和InputStream的转换

1. String --> InputStream

public static InputStream String2InputStream(String str) throws IOException{
   ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
   return stream;
}

 2. InputStream --> String

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)