2013-03-13 46 views
6

我正在使用Grails應用程序,它具有文件共享功能。它將文件上傳到服務器,並允許用戶從服務器下載文件。我用這下面的代碼:使用grails進行大文件下載

def file = new java.io.File(filePath) 
response.setContentType("application-xdownload") 
response.setHeader("Content-Disposition", "attachment;filename=${fileName}") 
response.getOutputStream() << new ByteArrayInputStream(file.getBytes()) 

此代碼工作正常對於小文件,但是當文件大小增加,即> 100MB,它給了我下面的錯誤:

java.lang.OutOfMemoryError: Java heap space 

所以,我能做些什麼來使我的應用程序能夠下載大文件? 感謝

回答

9

不是文件加載到內存中,取代

response.getOutputStream() << new ByteArrayInputStream(file.getBytes()) 

有了:

file.withInputStream { response.outputStream << it } 
+0

謝謝!非常棒! – 2013-03-13 08:45:53