1. import java.io.ByteArrayInputStream;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4.   
  5. import java.io.IOException;  
  6.   
  7. import java.io.InputStream;  
  8.   
  9. public class InputStreamUtils {  
  10.   
  11.     final static int BUFFER_SIZE = 4096;  
  12.   
  13.     // 将InputStream转换成String  
  14.     public static String InputStreamTOString(InputStream in) throws Exception {  
  15.   
  16.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  17.   
  18.         byte[] data = new byte[BUFFER_SIZE];  
  19.   
  20.         int count = -1;  
  21.   
  22.         while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)  
  23.   
  24.             outStream.write(data, 0, count);  
  25.   
  26.         data = null;  
  27.   
  28.         return new String(outStream.toByteArray(), "ISO-8859-1");  
  29.   
  30.     }  
  31.   
  32.     // 将InputStream转换成某种字符编码的String  
  33.     public static String InputStreamTOString(InputStream in, String encoding)  
  34.             throws Exception {  
  35.   
  36.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  37.   
  38.         byte[] data = new byte[BUFFER_SIZE];  
  39.   
  40.         int count = -1;  
  41.   
  42.         while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)  
  43.   
  44.             outStream.write(data, 0, count);  
  45.   
  46.         data = null;  
  47.   
  48.         return new String(outStream.toByteArray(), "ISO-8859-1");  
  49.   
  50.     }  
  51.   
  52.     // 将String转换成InputStream  
  53.     public static InputStream StringTOInputStream(String in) throws Exception {  
  54.   
  55.         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));  
  56.   
  57.         return is;  
  58.   
  59.     }  
  60.   
  61.     // 将InputStream转换成byte数组  
  62.     public static byte[] InputStreamTOByte(InputStream in) throws IOException {  
  63.   
  64.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  65.   
  66.         byte[] data = new byte[BUFFER_SIZE];  
  67.   
  68.         int count = -1;  
  69.   
  70.         while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)  
  71.   
  72.             outStream.write(data, 0, count);  
  73.   
  74.         data = null;  
  75.   
  76.         return outStream.toByteArray();  
  77.   
  78.     }  
  79.   
  80.     // 将byte数组转换成InputStream  
  81.     public static InputStream byteTOInputStream(byte[] in) throws Exception {  
  82.   
  83.         ByteArrayInputStream is = new ByteArrayInputStream(in);  
  84.   
  85.         return is;  
  86.   
  87.     }  
  88.   
  89.     // 将byte数组转换成String  
  90.     public static String byteTOString(byte[] in) throws Exception {  
  91.   
  92.         InputStream is = byteTOInputStream(in);  
  93.   
  94.         return InputStreamTOString(is);  
  95.   
  96.     }  
  97.   
posted on 2017-11-20 17:04  aysfeelings  阅读(183)  评论(0)    收藏  举报