Play framework - pass multiple images in a post request

陌路散爱 提交于 2019-12-25 02:58:04

问题


WHAT I DID:

I am developing Rest web services using POST method in the play framework(Using Java). I did create simple web service POST API and also called it from the client side.

WHAT I WANT:

In this I want to pass multiple images as parameters in the request to the service from the client side(android/IOS/web). But I didn't get any APIs/Tutorials regarding this.

I did try to pass one image to the service. But when I pass the Image request, I am getting the null in the line "FilePart file = body.getFile("img1")".

In Application.java:

public static Result sampleMethod() {
    ObjectNode result = Json.newObject();
    result.put("message", "WS - Sample method");
    MultipartFormData body = request().body().asMultipartFormData();
    FilePart file = body.getFile("img1");
    return ok(result);
}

In routes file:

POST /sampleMethod controllers.Application.sampleMethod()

In Client.java:

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost:9000/sampleMethod");
        File file = new File("<<image path>>");

        if(file.exists())
            System.out.println("File exist");
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "image/jpeg");
        mpEntity.addPart("img1", cbFile);


        httppost.setEntity(mpEntity);
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println(response.getStatusLine());

When I add the log "Logger.info(request().body().toString());" I am getting the below value. Is there anything problem in the request?

DefaultRequestBody(None,None,None,None,None,Some(MultipartFormData(Map(),List(),List(BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map())),List())),false)

Has anybody done this already? Can anyone help me do this?


回答1:


You can post multiple files and get them from request this way:

MultipartFormData body = request().body().asMultipartFormData();
FilePart picture1 = body.getFile("file1");
FilePart picture2 = body.getFile("file2");

"file1" and "file2" are the names of the post query parameters.

Speaking of the tutorials: Java file upload on Play 2.0



来源:https://stackoverflow.com/questions/30190052/play-framework-pass-multiple-images-in-a-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!