问题
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