Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

java - 在Java中将InputStream转换为字节数组(Convert InputStream to byte array in Java)

如何将整个InputStream读入字节数组?

  ask by JGC translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Apache Commons IO to handle this and similar tasks.

(您可以使用Apache Commons IO处理此任务和类似任务。)

The IOUtils type has a static method to read an InputStream and return a byte[] .

(IOUtils类型具有静态方法来读取InputStream并返回byte[] 。)

InputStream is;
byte[] bytes = IOUtils.toByteArray(is);

Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray() .

(在内部,这将创建一个ByteArrayOutputStream并将字节复制到输出,然后调用toByteArray() 。)

It handles large files by copying the bytes in blocks of 4KiB.

(它通过复制4KiB块中的字节来处理大型文件。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...