【问题标题】:Play framework - pass multiple images in a post request播放框架 - 在发布请求中传递多个图像
【发布时间】:2015-05-12 11:52:02
【问题描述】:

我做了什么:

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

我想要什么:

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

我确实尝试将一张图片传递给服务。但是当我通过图像请求时,我在"FilePart file = body.getFile("img1")" 行中得到了空值。

在 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 中:

        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());”我得到以下值。请求有什么问题吗?

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)

有人已经这样做了吗?谁能帮我做这个?

【问题讨论】:

标签: java web-services playframework playframework-2.0 http-post


【解决方案1】:

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

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

“file1”和“file2”是post查询参数的名称。

讲教程:Java file upload on Play 2.0

【讨论】:

  • 感谢您的回复。我这样做是为了传递一张图片。但我得到了空值。你能检查一下吗? stackoverflow.com/q/30170208/1584121
  • 在问题部分查看我的评论。
  • 我已经删除了之前的帖子并在这个问题中进行了更新。你能检查我更新的问题和日志吗?
  • 日志不是你想要的。请调试应用程序并检查实际参数及其所具有的参数;虽然请求中似乎有一个文件参数,但它需要调试。
  • 我确实调试了客户端应用程序并在标题中获得了以下值。 -->request POST localhost:9000/sampleMethod HTTP/1.1, -->org.apache.http.client.methods.HttpPost@60dd1773, -->Accept: application/json, -->enctype: multipart/form-data, - ->accept-charset: UTF-8 并且我可以在请求中看到多部分实体。
猜你喜欢
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-02
  • 2015-06-18
相关资源
最近更新 更多