【发布时间】: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)
有人已经这样做了吗?谁能帮我做这个?
【问题讨论】:
-
您究竟需要什么帮助?
-
@ConstantineNovykov 我已经更新了我的问题。
-
很难在两个不同的帖子上关注同一个问题 - 如果您可以在此处获取代码并关闭另一个问题,那就太好了。至于代码 - 你可以在调试中检查请求,看看那里是否确实有一个多部分参数。
-
@ConstantineNovykov 请帮我解决这个问题stackoverflow.com/q/30306504/1584121
标签: java web-services playframework playframework-2.0 http-post