小编Him*_*ndu的帖子

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

java out-of-memory

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

out-of-memory ×1