播放2.0.8如何从MultipartFormData获取FilePart?

时间:2013-11-21 13:06:17

标签: java html5 playframework playframework-2.0

无法通过播放表单类型获取文件。和 视图html是:

<div data-role="appviewpage" data-title="Upload Scenario Config">
    <div data-role="content">

        <form action="/upload/configfile.html" method="POST" enctype="multipart/form-data">

            <input type="file" name="picture">

            <p>
                <input type="submit" value="save">
            </p>

        </form>
    </div>
</div>

,应用功能是:

@BodyParser.Of(value = BodyParser.Text.class, maxLength = 100 * 1024)
    public static Result uploadConfigFile() {
        Http.MultipartFormData body = request().body().asMultipartFormData();
        FilePart picture = body.getFile("picture");

        if (picture != null) {
          String fileName = picture.getFilename();
          String contentType = picture.getContentType(); 
          File file = picture.getFile();
          return ok("File uploaded");
        } else {
            return ok("this is not support now");
        }

    }

并且映射器是:

POST   /upload/configfile.html          controllers.Application.uploadConfigFile()

但回复是:

Caused by: java.lang.NullPointerException: null
        at controllers.Application.uploadConfigFile(Application.java:340)

1 个答案:

答案 0 :(得分:0)

我的问题非常相似,但是在游戏2.2.0。

尝试在您的视图中添加html此语句:'enctype - &gt; “多部分/格式数据” 例如:

@form(routes.Application.uploadConfigFile(), 'enctype -> "multipart/form-data"){
..
<input type="file" name="picture">

<p>
    <input type="submit" value="save">
</p>
...
}

编辑: 我看到@Musa是第一个:)