I am working on a grails application, it has a file sharing feature. It uploads files onto the server and allows user to download the file from server. I used the following code for this :
def file = new java.io.File(filePath)
response.setContentType( "application-xdownload")
response.setHeader("Content-Disposition", "attachment;filename=${fileName}")
response.getOutputStream() << new ByteArrayInputStream(file.getBytes())
This code works fine for small files but when the size of file is increased, i.e. >100MB, it gives me the following error :
java.lang.OutOfMemoryError: Java heap space
So, what can I do to make my application be able to download large files ? Thanks
Instead of loading the file into memory, replace
response.getOutputStream() << new ByteArrayInputStream(file.getBytes())
With:
file.withInputStream { response.outputStream << it }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With