如何获取 ByteArrayInputStream 并将其内容另存为文件系统上的文件

2022-09-03 00:21:55

我有一个以ByteArrayInputStream形式存在的图像。我想把它变成一个可以保存到文件系统中某个位置的东西。

我一直在兜圈子,你能帮帮我吗?


答案 1

如果您已经在使用Apache commons-io,则可以使用:

 IOUtils.copy(byteArrayInputStream, new FileOutputStream(outputFileName));

答案 2
InputStream in = //your ByteArrayInputStream here
OutputStream out = new FileOutputStream("filename.jpg");

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
}
in.close();
out.close();