java从输入流中获取数据并返回字节数组

 1 import java.io.ByteArrayOutputStream;
 2 import java.io.InputStream;
 3 //从输入流中获取数据并以字节数组返回
 4 public class StreamTool {
 5     /**
 6      * 从输入流获取数据
 7      * @param inputStream
 8      * @return
 9      * @throws Exception
10      */
11     public static byte[] readInputStream(InputStream inputStream) throws Exception{
12         byte[] buffer = new byte[1024];
13         int len = -1;
14         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
15         while((len = inputStream.read(buffer)) != -1){
16             outputStream.write(buffer, 0, len);
17         }
18         outputStream.close();
19         inputStream.close();
20         return outputStream.toByteArray();
21     }
22 }