【问题标题】:Grails 3 upload file limit sizeGrails 3上传文件限制大小
【发布时间】:2018-04-11 14:13:34
【问题描述】:

我在做什么以及发生了什么?


我正在尝试在 grails 中上传文件并下载它们。制作完成后,我仍然面临一个问题,当文件很大时。这是一个例外:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: 
the request was rejected because its size (3553808) exceeds the configured maximum (128000)

我的尝试和结果:


我在this question发现了这个问题,答案是放一些配置变量

grails:
    controllers:
        upload:
            maxFileSize: (10 * 1024 * 1024)
            maxRequestSize: (10 * 1024 * 1024)

但仍然出现同样的错误。我还尝试将一些依赖项添加为said here。或者关闭 IDE 并重建。什么都解决不了。


有人遇到过这个问题并且可以解决吗?

【问题讨论】:

  • 您是否尝试过链接问题的任何其他答案,例如this bean configuration
  • 我试过了,但无法解决导入问题
  • 我结合compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.3'再次尝试。这样就解决了问题,但是像以前一样在请求中找不到文件。
  • 用bean配置的回答也说不需要任何依赖或额外的配置。
  • 您的部署容器是否强制执行此限制?您如何运行/部署您的应用程序?

标签: grails file-upload grails-controller grails-3.3 grails-config


【解决方案1】:

问题在于配置变量的分配。我在没有运算符的情况下分配它们:

        maxFileSize: 10485760
        maxRequestSize: 10485760

代替:

        maxFileSize: (10 * 1024 * 1024)
        maxRequestSize: (10 * 1024 * 1024)

这就是我解决问题的方法。

【讨论】:

  • 如果上传大小大于配置的大小,您是否能够捕获 FileUploadBase$SizeLimitExceededException?
  • 对不起!我不记得那个行为了,超过 2 年没有碰过那个 temary :S
【解决方案2】:

使用 grails 3.2 及更高版本对我来说工作正常

我们将允许上传 25MB 的文件。

25 * 1024 * 1024 = 26.214.400 字节

我的/grails-app/conf/application.yml

grails:
 controllers:
  upload:
    maxFileSize: 26214400
    maxRequestSize: 26214400

我的my.gsp 文件

<g:form action="saveImage" enctype="multipart/form-data" method="POST">

    <input type="file" placeholder="Select file" name="file">

    <g:submitButton value="Upload File"/>

</g:form>

我的controller 操作

def saveImage(){
def fileName = ''
def uploadedFile = request.getFile('file')

if (uploadedFile)
    if (!uploadedFile.empty) {
        try {

            int dot = uploadedFile.originalFilename.lastIndexOf('.');
            def fileExt = uploadedFile.originalFilename.substring(dot + 1);
            fileName = ("myFile." + fileExt).toString()
            def basePath = ''
            if (Environment.current == Environment.PRODUCTION) {
                basePath ='/var/local/prj/uploads/' // this works with production and tested on Ubuntu OS
            } else {
                basePath = grailsApplication.mainContext.servletContext.getRealPath('/uploads/') // this will take your project directory path Project\src\main\webapp\uploads folder
            }
            uploadedFile.transferTo(new File(basePath + fileName))
        } catch (Exception e) {
            e.printStackTrace()
        }
    }
    // Your redirect code here
}

下载文件操作

def file = new File("Your file path")
if (file.exists()) {
    response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is
    response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"")
    response.outputStream << file.bytes
} else 
    render "File does not exist!"

希望对你有帮助

【讨论】:

  • 注意:使用 IntelliJ IDEA 2016/2017/2018 :)
  • @IgniteCoders 您要上传的文件大小是多少?
  • 我正在尝试上传不同大小的文件。在 10 到 2 Mb 之间。不过我终于解决了,看看我的回答。
  • 我想我也提到了同样的:) 检查maxFileSize: 26214400 and maxRequestSize: 26214400
  • 是的,但我从没想过这可能是问题所在。因为我没有收到任何错误,所以配置只是默认并跳过我的。
猜你喜欢
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2011-01-29
  • 2017-03-09
  • 2011-01-13
  • 1970-01-01
  • 2022-10-26
  • 1970-01-01
相关资源
最近更新 更多