从InputStream到String_写成函数

 1 String result = readFromInputStream(inputStream);//调用处
 2 //将输入流InputStream变为String
 3     public String readFromInputStream(InputStream in) throws IOException {
 4         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 5         byte[] buffer = new byte[1024];
 6         int len = -1;
 7         while ((len = in.read(buffer)) != -1) {
 8             baos.write(buffer, 0, len);
 9         }
10         baos.close();
11         in.close();
12         
13         byte[] lens = baos.toByteArray();
14         String result = new String(lens,"UTF-8");//内容乱码处理
15         
16         return result;
17     
18     }

 

posted on 2014-07-15 09:47  随风浪子的博客  阅读(323)  评论(0编辑  收藏  举报

导航