本文将介绍InputStream与String,Byte之间的相互转换。以代码来说明:
**[html]** [view plain](http://blog.csdn.net/cjjky/article/details/6892443#) [copy](http://blog.csdn.net/cjjky/article/details/6892443#)
<div>
<embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" height="18" align="middle" name="ZeroClipboardMovie_1">
</embed>
</div>
</div>
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
-
- /**
- *
- * @author Andy.Chen
- * @mail Chenjunjun.ZJ@gmail.com
- *
- */
- public class InputStreamUtils {
-
- final static int <span class="attribute">BUFFER_SIZE</span> = <span class="attribute-value">4096</span>;
-
- /**
- * 将InputStream转换成String
- * @param in InputStream
- * @return String
- * @throws Exception
- *
- */
- public static String InputStreamTOString(InputStream in) throws Exception{
-
- ByteArrayOutputStream <span class="attribute">outStream</span> = <span class="attribute-value">new</span> ByteArrayOutputStream();
- byte[] <span class="attribute">data</span> = <span class="attribute-value">new</span> byte[BUFFER_SIZE];
- int <span class="attribute">count</span> = -1;
- while((<span class="attribute">count</span> = <span class="attribute-value">in</span>.read(data,0,BUFFER_SIZE)) != -1)
- outStream.write(data, 0, count);
-
- <span class="attribute">data</span> = <span class="attribute-value">null</span>;
- return new String(outStream.toByteArray(),”ISO-8859-1″);
- }
-
- /**
- * 将InputStream转换成某种字符编码的String
- * @param in
- * @param encoding
- * @return
- * @throws Exception
- */
- public static String InputStreamTOString(InputStream in,String encoding) throws Exception{
-
- ByteArrayOutputStream <span class="attribute">outStream</span> = <span class="attribute-value">new</span> ByteArrayOutputStream();
- byte[] <span class="attribute">data</span> = <span class="attribute-value">new</span> byte[BUFFER_SIZE];
- int <span class="attribute">count</span> = -1;
- while((<span class="attribute">count</span> = <span class="attribute-value">in</span>.read(data,0,BUFFER_SIZE)) != -1)
- outStream.write(data, 0, count);
-
- <span class="attribute">data</span> = <span class="attribute-value">null</span>;
- return new String(outStream.toByteArray(),”ISO-8859-1″);
- }
-
- /**
- * 将String转换成InputStream
- * @param in
- * @return
- * @throws Exception
- */
- public static InputStream StringTOInputStream(String in) throws Exception{
-
- ByteArrayInputStream <span class="attribute">is</span> = <span class="attribute-value">new</span> ByteArrayInputStream(in.getBytes(“ISO-8859-1”));
- return is;
- }
-
- /**
- * 将InputStream转换成byte数组
- * @param in InputStream
- * @return byte[]
- * @throws IOException
- */
- public static byte[] InputStreamTOByte(InputStream in) throws IOException{
-
- ByteArrayOutputStream <span class="attribute">outStream</span> = <span class="attribute-value">new</span> ByteArrayOutputStream();
- byte[] <span class="attribute">data</span> = <span class="attribute-value">new</span> byte[BUFFER_SIZE];
- int <span class="attribute">count</span> = -1;
- while((<span class="attribute">count</span> = <span class="attribute-value">in</span>.read(data,0,BUFFER_SIZE)) != -1)
- outStream.write(data, 0, count);
-
- <span class="attribute">data</span> = <span class="attribute-value">null</span>;
- return outStream.toByteArray();
- }
-
- /**
- * 将byte数组转换成InputStream
- * @param in
- * @return
- * @throws Exception
- */
- public static InputStream byteTOInputStream(byte[] in) throws Exception{
-
- ByteArrayInputStream <span class="attribute">is</span> = <span class="attribute-value">new</span> ByteArrayInputStream(in);
- return is;
- }
-
- /**
- * 将byte数组转换成String
- * @param in
- * @return
- * @throws Exception
- */
- public static String byteTOString(byte[] in) throws Exception{
-
- InputStream <span class="attribute">is</span> = <span class="attribute-value">byteTOInputStream</span>(in);
- return InputStreamTOString(is);
- }
-
- }
💬 评论