This article describes the conversion between inputstream and string and byte. ToCodeTo describe:
[HTML] view plain
Copy
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 buffer_size = 4096;
/**
* Convert inputstream to string
* @ Param in inputstream
* @ Return string
* @ Throws exception
*
*/
Public static string inputstreamtostring (inputstream in) throws exception {
Bytearrayoutputstream outstream = new bytearrayoutputstream ();
Byte [] DATA = new byte [buffer_size];
Int COUNT =-1;
While (COUNT = in. Read (data, 0, buffer_size ))! =-1)
Outstream. Write (data, 0, count );
Data = NULL;
Return new string (outstream. tobytearray (), "ISO-8859-1 ");
}
/**
* Convert inputstream to a character-encoded string
* @ Param in
* @ Param Encoding
* @ Return
* @ Throws exception
*/
Public static string inputstreamtostring (inputstream in, string encoding) throws exception {
Bytearrayoutputstream outstream = new bytearrayoutputstream ();
Byte [] DATA = new byte [buffer_size];
Int COUNT =-1;
While (COUNT = in. Read (data, 0, buffer_size ))! =-1)
Outstream. Write (data, 0, count );
Data = NULL;
Return new string (outstream. tobytearray (), "ISO-8859-1 ");
}
/**
* Convert string to inputstream
* @ Param in
* @ Return
* @ Throws exception
*/
Public static inputstream stringtoinputstream (string in) throws exception {
Bytearrayinputstream is = new bytearrayinputstream (in. getbytes ("ISO-8859-1 "));
Return is;
}
/**
* Convert inputstream to byte array
* @ Param in inputstream
* @ Return byte []
* @ Throws ioexception
*/
Public static byte [] inputstreamtobyte (inputstream in) throws ioexception {
Bytearrayoutputstream outstream = new bytearrayoutputstream ();
Byte [] DATA = new byte [buffer_size];
Int COUNT =-1;
While (COUNT = in. Read (data, 0, buffer_size ))! =-1)
Outstream. Write (data, 0, count );
Data = NULL;
Return outstream. tobytearray ();
}
/**
* Convert byte array to inputstream
* @ Param in
* @ Return
* @ Throws exception
*/
Public static inputstream bytetoinputstream (byte [] In) throws exception {
Bytearrayinputstream is = new bytearrayinputstream (in );
Return is;
}
/**
* Convert byte array to string
* @ Param in
* @ Return
* @ Throws exception
*/
Public static string bytetostring (byte [] In) throws exception {
Inputstream is = bytetoinputstream (in );
Return inputstreamtostring (is );
}
}
Http://blog.csdn.net/cjjky/article/details/6892443