Edd*_*ark 6 grails groovy file download
我正在处理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())
Run Code Online (Sandbox Code Playgroud)
此代码适用于小文件,但当文件大小增加,即> 100MB时,它会给我以下错误:
java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)
那么,我该怎么做才能使我的应用程序能够下载大文件?谢谢
不要将文件加载到内存中,而是替换
response.getOutputStream() << new ByteArrayInputStream(file.getBytes())
Run Code Online (Sandbox Code Playgroud)
附:
file.withInputStream { response.outputStream << it }
Run Code Online (Sandbox Code Playgroud)