java中如何在没有内存问题的情况下下载大文件
当我尝试从服务器下载 260MB 的大文件时,出现以下错误:java.lang.OutOfMemoryError: Java heap space.
我确信我的堆大小小于 252MB。有什么方法可以在不增加堆大小的情况下下载大文件吗?
如何下载大文件而不出现此问题?我的代码如下:
String path= "C:/temp.zip";
response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");
byte[] buf = new byte[1024];
try {
File file = new File(path);
long length = file.length();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
ServletOutputStream out = response.getOutputStream();
while ((in != null) && ((length = in.read(buf)) != -1)) {
out.write(buf, 0, (int) length);
}
in.close();
out.close();
When I am trying to download a large file which is of 260MB from server, I get this error: java.lang.OutOfMemoryError: Java heap space.
I am sure my heap size is less than 252MB. Is there any way I can download large files without increasing heap size?
How I can download large files without getting this issue? My code is given below:
String path= "C:/temp.zip";
response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");
byte[] buf = new byte[1024];
try {
File file = new File(path);
long length = file.length();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
ServletOutputStream out = response.getOutputStream();
while ((in != null) && ((length = in.read(buf)) != -1)) {
out.write(buf, 0, (int) length);
}
in.close();
out.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以看到您可能在两个地方增加内存使用量:
对于#1,我建议通过
FileInputStream
直接从文件中读取,而不使用BufferedInputStream
。请先尝试此操作,看看是否可以解决您的问题。即:而不是:
如果#1 不能解决问题,您可以尝试在写入大量数据后定期刷新输出流(如有必要,减少块大小):
即:
There are 2 places where I can see you could potentially be building up memory usage:
For #1 I would suggest reading directly from the file via
FileInputStream
without theBufferedInputStream
. Try this first and see if it resolves your issue. ie:instead of:
If #1 does not resolve the issue, you could try periodically flushing the output stream after so much data is written (decrease chunk size if necessary):
ie:
不幸的是,您没有提到
out
是什么类型。如果您有内存问题,我猜是 ByteArrayOutpoutStream 。因此,将其替换为 FileOutputStream 并将要下载的字节直接写入文件。顺便说一句,不要使用逐字节读取的
read()
方法。请改用read(byte[] arr)
。这要快得多。Unfortunately you have not mentioned what type
out
is. If you have memory issues I guess it isByteArrayOutpoutStream
. So, replace it byFileOutputStream
and write the byte you are downloading directly to file.BTW, do not use
read()
method that reads byte-by-byte. Useread(byte[] arr)
instead. This is much faster.首先,您可以从 while 语句中删除 (in != null) ,这是不必要的。其次,尝试删除 BufferedInputStream 并执行以下操作:
First you can remove the (in != null) from your while statement, it's unnecessary. Second, try removing the BufferedInputStream and just do:
您显示的代码没有任何问题(就内存使用而言)。 servlet 容器配置为缓冲整个响应(查看 web.xml 配置),或者内存在其他地方泄漏。
There's nothing wrong (in regard to memory usage) with the code you're show. Either the servlet container is configured to buffer the entire response (look at the
web.xml
configuration), or the memory is being leaked elsewhere.