我需要接收一个仅包含 2 个参数的 HTTP Post Multipart:
- 一个 JSON 字符串
- 二进制文件
设置body的正确方法是什么? 我将使用 Chrome REST 控制台测试 HTTP 调用,所以我想知道是否正确的解决方案是为 JSON 参数和二进制文件设置“标签”键。
在服务器端,我使用的是 Resteasy 2.x,我将像这样读取 Multipart 正文:
@POST
@Consumes("multipart/form-data")
public String postWithPhoto(MultipartFormDataInput multiPart) {
Map <String, List<InputPart>> params = multiPart.getFormDataMap();
String myJson = params.get("myJsonName").get(0).getBodyAsString();
InputPart imagePart = params.get("photo").get(0);
//do whatever I need to do with my json and my photo
}
这是要走的路吗? 使用标识特定内容处置的 key “myJsonName”检索我的 JSON 字符串是否正确? 有没有其他方法可以在一个 HTTP 多部分请求中接收这 2 个内容?
提前致谢
最佳答案
如果我对您的理解正确,您想从 HTTP/REST 控制台手动编写多部分请求。多部分格式很简单;简单介绍可以看in the HTML 4.01 spec .您需要提出一个边界,即在内容中找不到的字符串,比如 HereGoes
。您设置请求 header Content-Type: multipart/form-data;边界=HereGoes
。那么这应该是一个有效的请求正文:
--HereGoes
Content-Disposition: form-data; name="myJsonString"
Content-Type: application/json
{"foo": "bar"}
--HereGoes
Content-Disposition: form-data; name="photo"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
<...JPEG content in base64...>
--HereGoes--
关于java - REST - 带有 JSON 的 HTTP Post Multipart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081079/