Download large file using grails

I am working on a grails application, it has file sharing function. It uploads files to the server and allows the user to download the file from the server. For this, I used the following code:

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 is great for small files, but when the file size increases, i.e.> 100 MB, it causes the following error:

 java.lang.OutOfMemoryError: Java heap space 

So what can I do so that my application can upload large files? Thanks

+6
source share
1 answer

Instead of loading the file into memory, replace

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

WITH

 file.withInputStream { response.outputStream << it } 
+9
source

All Articles