Play Framework 2.0中的文件大小限制

时间:2014-04-29 13:11:10

标签: java scala file-upload playframework playframework-2.0

我试图检查播放应用程序(2.0.3)中上传文件的大小。

我尝试过Scala和Java控制器,但我每次都有同样奇怪的行为......我设法检测到文件太大,但当我尝试返回响应时,请求挂起永远,用户不知道请求无效。

在Java中:

@BodyParser.Of(value = BodyParser.MultipartFormData.class, maxLength = 10 * 1024 * 1024)
pulic static Result upload() {
  if(request().body().isMaxSizeExceeded()) {
    return badRequest("Too much data!"); // this is not returned
  } else {
    ok("Got body: " + request().body().asText()); 
  }
}

在Scala中:

def upload = Action(parse.maxLength(10 * 1024 * 1024, parse.multipartFormData)) { request =>
    request.body match {
        case Left(MaxSizeExceeded(length)) => {
            Logger.error("MaxSizeExceeded")
            BadRequest("Your file is too large, we accept just " + length + " bytes!")
        }
        case Right(multipartForm) => {
            // Do stuff to handle the file
        }
    }
}

HTML模板:

@(httpPath: java.lang.String) @main(httpPath) {

    @helper.form(action = routes.Application.upload(), 'enctype -> "multipart/form-data", 'class -> "form-horizontal", 'id -> "form") {

        <div id="well" class="well">

                <h1>Upload</h1>
                <div id="formGroup" class="control-group">
                    <label class="control-label" for="file">Select file : </label>
                    <div class="controls">
                        <input id="file" name="file" type="file" style="display: none" /> 

                        <div class="input-append">
                            <input id="txtFile" type="text" class="required" readonly="true"/>
                            <span class="btn" onclick="$('#file').click();">Browse</span>
                        </div>

                        <script type="text/javascript">
                             $('#file').change(function() { 
                                 $('#txtFile').val($(this).val()); 
                             }); 
                         </script>
                    </div>
                </div>

                <button id="submit" type="submit" class="btn btn-primary" >Upload</button>
        </div>
    }
}

记录:

2014-04-30 16:46:44,791 - [[trace]] - play - New I/O  worker #4 - Serving this request with: Action(parser=BodyParser(maxLength=1048576, wrapping=BodyParser(multipartFormData))) -
2014-04-30 16:46:44,996 - [[trace]] - play - play-akka.actor.promises-dispatcher-60 - Invoking action with request: POST /upload -
2014-04-30 16:46:44,998 - [[error]] - application - play-akka.actor.actions-dispatcher-10 - MaxSizeExceeded java -
2014-04-30 16:46:44,999 - [[trace]] - play - play-akka.actor.actions-dispatcher-10 - Sending simple result: SimpleResult(400, Map(Content-Type -> text/plain; charset=utf-8, Set-Cookie -> )) -

我看到其他开发人员面临同样的问题(例如https://groups.google.com/forum/#!topic/play-framework/wuXnoXN5GZ0)。 这是2.0.x中的已知问题吗?我做错了吗?

由于

1 个答案:

答案 0 :(得分:1)

通过将Play版本升级到2.2.1,问题就会消失。

我尝试使用最新发布的2.0.x版本(2.0.8),但该错误仍然存​​在。

显然,它已在2.1.x中修复。