使用 grails 下载大文件 [英] Large file download using grails
本文介绍了使用 grails 下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个 grails 应用程序,它具有文件共享功能.它将文件上传到服务器并允许用户从服务器下载文件.我为此使用了以下代码:
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())
此代码适用于小文件,但当文件大小增加时,即 >100MB,它给我以下错误:
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())
与:
file.withInputStream { response.outputStream << it }
这篇关于使用 grails 下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文