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,我建议直接从文件中读取,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)
不幸的是您没有提到什么类型out。如果您有内存问题,我想是ByteArrayOutpoutStream。因此,将其替换为FileOutputStream将要下载的字节直接写入文件。
顺便说一句,不要使用read()逐字节读取方法。使用read(byte[] arr)代替。这要快得多。
| 归档时间: |
|
| 查看次数: |
24552 次 |
| 最近记录: |