У меня есть изображение, которое является в форме ByteArrayInputStream. Я хочу взять это и сделать его чем-то, что я могу сохранить к местоположению в моей файловой системе.
Я бродил вокруг в кругах, могли Вы выручать меня.
Если вы уже используете Apache commons-io, вы можете сделать это с помощью:
IOUtils.copy(byteArrayInputStream, new FileOutputStream(outputFileName));
Вы можете использовать следующий код:
ByteArrayInputStream input = getInputStream();
FileOutputStream output = new FileOutputStream(outputFilename);
int DEFAULT_BUFFER_SIZE = 1024;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE);
while (n >= 0) {
output.write(buffer, 0, n);
n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE);
}
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();
ByteArrayInputStream stream = <<Assign stream>>;
byte[] bytes = new byte[1024];
stream.read(bytes);
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("FileLocation")));
writer.write(new String(bytes));
writer.close();
Buffered Writer улучшит производительность при записи файлов по сравнению с FileWriter.