开发者

Save InputStream to ByteArray

开发者 https://www.devze.com 2023-03-22 18:47 出处:网络
My task is: Clients connect to ServerSocket and send files with any encoding what they want(UTF-8, ISO-8859-5, CP1251 e.g.).

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

精彩评论

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