1

我在做什么,发生了什么?


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

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

我尝试了什么和结果:


我发现这个问题之前问过这个问题,答案是放置一些配置变量

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

但仍然得到同样的错误。我还尝试添加一些依赖项,如此处所述。或者关闭 IDE 并重建。没有什么可以解决的。


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

4

2 回答 2

1

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

        maxFileSize: 10485760
        maxRequestSize: 10485760

代替:

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

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

于 2018-04-12T11:58:20.833 回答
0

使用 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!"

希望这可以帮助你

于 2018-04-11T18:55:06.660 回答