大文件下载使用grails [英] Large file download using grails
本文介绍了大文件下载使用grails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def file = new java.io.File(filePath)
response。 setContentType(application-xdownload)
response.setHeader(Content-Disposition,attachment; filename = $ {fileName})
response.getOutputStream()<<新的ByteArrayInputStream(file.getBytes())
此代码适用于小文件,但是当文件增加,即> 100MB,它给我以下错误:
java.lang.OutOfMemoryError:Java堆空间
那么,如何使我的应用程序能够下载大文件?
谢谢
解决方案
而不是将文件加载到内存中,替换
response.getOutputStream()<<新的ByteArrayInputStream(file.getBytes())
With:
file.withInputStream {response.outputStream<<< it}
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 }
这篇关于大文件下载使用grails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文