播放框架 - 在帖子请求中传递多个图像 [英] Play framework - pass multiple images in a post request

查看:153
本文介绍了播放框架 - 在帖子请求中传递多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么:

我正在使用Play框架中的POST方法开发Rest Web服务(使用Java)。我确实创建了简单的Web服务POST API,并从客户端调用它。

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.

我想要的是什么:

在此我想从客户端(android / IOS / web)将多个图像作为参数传递给服务。但我没有得到任何关于此的API /教程。

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.

我确实尝试将一张图片传递给该服务。但是当我传递Image请求时,我在行FilePart file = body.getFile(img1)

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")".

在Application.java中:

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);
}

在路线档案中:

POST /sampleMethod controllers.Application.sampleMethod()

在Client.java中:

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());

当我添加日志Logger.info(request()。body()。toString() );我得到以下价值。请求中有什么问题吗?

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?

推荐答案

您可以发布多个文件并通过这种方式获取它们:

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和file2是帖子查询参数的名称。

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

说到教程: Play 2.0上的Java文件上传

这篇关于播放框架 - 在帖子请求中传递多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆