如何在java中下载没有内存问题的大文件

Him*_*ndu 14 java out-of-memory

当我尝试从服务器下载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();
Run Code Online (Sandbox Code Playgroud)

JJ.*_*JJ. 18

我可以看到有两个地方可能会增加内存使用量:

  1. 在缓冲区中读取输入文件.
  2. 在缓冲区写入输出流(HTTPOutputStream?)

对于#1,我建议直接从文件中读取,FileInputStream而不是BufferedInputStream.首先尝试这一点,看看它是否能解决您的问题.即:

FileInputStream in = new FileInputStream(file);   
Run Code Online (Sandbox Code Playgroud)

代替:

BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));   
Run Code Online (Sandbox Code Playgroud)

如果#1无法解决问题,您可以尝试在写入如此多的数据后定期刷新输出流(如果需要,减少块大小):

即:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 一个小问题是上面的代码.我们需要在finally块中添加一个flush,如果是try-catch,它包围代码或while循环的末尾.否则它不会刷新完整的字节.否则可能会引起一些问题. (2认同)

Ale*_*exR 5

不幸的是您没有提到什么类型out。如果您有内存问题,我想是ByteArrayOutpoutStream。因此,将其替换为FileOutputStream将要下载的字节直接写入文件。

顺便说一句,不要使用read()逐字节读取方法。使用read(byte[] arr)代替。这要快得多。