Uploading a large file (>1GB) to grails3

心不动则不痛 提交于 2021-02-10 19:41:31

问题


Uploading a large file (>1GB) to a grails, I only need to access it via stream, no need to save the entire file to disk or RAM. However how can I access the upload stream? I tried with an Interceptor:

class UploadLargeFileInterceptor {

int order = HIGHEST_PRECEDENCE

UploadLargeFileInterceptor() {
    match(controller:"apiv2", action:"uploadLarge")
}

boolean before() {

    log.error('before')
    log.error(request.getInputStream().text.length() as String)
    true
}

boolean after() {

    log.error('after')
    true
}

void afterView() {
    // no-op
}
}

but stream length is always 0 and in the controller there is a multipart file which I am trying to avoid because it will store the whole file. Any ideas?


回答1:


No need for an interceptor. You can access user uploaded file stream like this inside controller:

MultipartFile uploadedFile = params.filename as MultipartFile
println "file original name" + uploadedFile.originalFilename
println "file content type" + uploadedFile.contentType
println "file size" + uploadedFile.size //consider as stream length
InputStream inputStream = uploadedFile.getInputStream() //access the input stream

Also make sure to allow max size in application.groovy like this:

grails.controllers.upload.maxFileSize = 1572864000 //1500MB (X MB * 1024 * 1024)
grails.controllers.upload.maxRequestSize = 1572864000 //1500MB

Or if you are running behind on webserver like nginx, tune that request & timeout config also.

With the above code and -Xms512m -Xmx1G heap size I uploaded 1.25Gb file with no issue. If you are uploading to file storage / cloud storage, make sure that lib uses stream like:

File: org.apache.commons.io.FileUtils.copyToFile(inputStream, new File('<file path>')) Azure: storageBlob.upload(inputStream, length)



来源:https://stackoverflow.com/questions/48163792/uploading-a-large-file-1gb-to-grails3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!