如何在java中下载没有内存问题的大文件 [英] how to download large files without memory issues in java
问题描述
当我尝试从服务器下载260MB的大文件时,我收到此错误: java.lang.OutOfMemoryError:Java堆空间。
我确定我的堆大小小于252MB。有没有什么办法可以在不增加堆大小的情况下下载大文件?
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();
推荐答案
有两个地方我可以看到你可能潜在的建立内存使用:
There are 2 places where I can see you could potentially be building up memory usage:
- 在读取输入文件的缓冲区中。
- 在缓冲区中写入输出流(HTTPOutputStream?)
对于#1我建议直接从文件中读取 FileInputStream
没有 BufferedInputStream
。首先尝试这一点,看看它是否能解决您的问题。 ie:
For #1 I would suggest reading directly from the file via FileInputStream
without the BufferedInputStream
. Try this first and see if it resolves your issue. ie:
FileInputStream in = new FileInputStream(file);
而不是:
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
如果#1无法解决问题,您可以尝试在这么多数据后定期刷新输出流编写(必要时减少块大小):
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:
try
{
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buf=new byte[8192];
int bytesread = 0, bytesBuffered = 0;
while( (bytesread = fileInputStream.read( buf )) > -1 ) {
out.write( buf, 0, bytesread );
bytesBuffered += bytesread;
if (bytesBuffered > 1024 * 1024) { //flush after 1MB
bytesBuffered = 0;
out.flush();
}
}
}
finally {
if (out != null) {
out.flush();
}
}
这篇关于如何在java中下载没有内存问题的大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!