【发布时间】:2011-08-18 11:36:49
【问题描述】:
当我尝试从服务器下载一个 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();
【问题讨论】:
-
你能增加 JVM 的堆大小吗?即:java -Xm512m -Xmx512m myClass
-
是的,但我想知道我是否可以在不增加 jvm 堆大小的情况下做到这一点,因为我们需要下载大约 1gb 到 10gb 的大文件
标签: java out-of-memory