我正在使用 inputstream 来读取数据并使用 byteArray 来获取大小。由于文件大小为 1.5 gb,我遇到了 outOfMemory 问题,需要 bytearray 中的新缓冲区。我已经检查了 stackoverflow 并用谷歌搜索了很多地方。我找不到获得尺寸的解决方案。请帮助我从输入流或任何其他最佳方式中获取大小。谢谢。
IOUtils 包 - 导入 org.apache.commons.io.IOUtils;
private void writeIntoResponse(String uri, String id, String fileName, HttpServletResponse response)
try (InputStream in = /*read(uri,id,fileName)reading from s3*/) { //1.5 gb file
response.setContentType("application/octet-stream");
byte[] bytes = IOUtils.toByteArray(in); //getting error out of memory issue
response.setHeader("Content-Length", String.valueOf((long)bytes.length)); // we needs to set bytes length as we need must
OutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(bytes);
response.flushBuffer();
}
catch (Exception e) {
////error
}
}
我收到如下错误。
Caused by: java.lang.OutOfMemoryError: Java heap space
at org.apache.commons.io.output.ByteArrayOutputStream.needNewBuffer(ByteArrayOutputStream.java:122) ~[commons-io-2.4.jar:2.4]
at org.apache.commons.io.output.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153) ~[commons-io-2.4.jar:2.4]