开发者

Save InputStream to ByteArray

My task is: Clients connect to ServerSocket and send files with any encoding what they want(UTF-8, ISO-8859-5, CP1251 e.g.). When Serv开发者_JAVA技巧er receive file content, script must insert it into MySQL.

As the encoding can be different, I need save file content like ByteArray(?).

Byt I dont know how get ByteArray from Socket.getInputStream().

Please help with this.

Thanks in advance!


ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] tmp = new byte[4096];
int ret = 0;

while((ret = inputStream.read(tmp)) > 0)
{
    bos.write(tmp, 0, ret);
}

byte[] myArray = bos.toByteArray();


Commons IO - http://commons.apache.org/io/

toByteArray(Reader input, String encoding) Get the contents of a Reader as a byte[] using the specified character encoding.

http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜