播放2如何从MultipartFormData获取DataPart

时间:2012-06-03 07:53:42

标签: playframework-2.0

如何从MultipartFormData获取DataPart?我找不到任何API来获得它。

Http.MultipartFormData formData = body.asMultipartFormData();

// simple form field
// there is NO getData() or something available
DataPart imageIdPart = formData.getData("dataKey");

// uploaded file
FilePart imagePart = formData.getFile("imageKey");

2 个答案:

答案 0 :(得分:12)

我不习惯在Play 2.0中使用Java,但这样的工作是什么?

@BodyParser.Of(BodyParser.MultipartFormData.class)
public static Result index() {
    Http.MultipartFormData multipartFormData = request().body().asMultipartFormData();

    //ask the multipart to be form url encoded... 
    //and get the data
    String[] data = multipartFormData.asFormUrlEncoded().get("dataKey");

    //which should not impact such call
    Http.MultipartFormData.FilePart image = multipartFormData.getFile("imageKey");

    return ok("Got image: " + image.getFilename());
}

答案 1 :(得分:5)

在scala中:

def index = Action(parse.multipartFormData) {
    request => {

     val dataKey = request.body.dataParts.get("dataKey")
     something something...

     Ok("Ok!")
}