多部分json post请求

jm81lzqq  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(376)

请看我的帖子:
我需要使用以下参数将图像发布到json ws:

Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: {number_of_bytes_in_entire_request_body} -- Check your rest client                   API's. Some would automatically determine content-length at runtime. Eg. jersey client api.
 --foo_bar_baz
Content-Type: application/json;
{
"filename": "cloudx.jpg"
}
--foo_bar_baz
Content-Type: image/jpeg
{JPEG data}
--foo_bar_baz--

我正在构建android应用程序,我需要编写请求将图像发送到上面的ws。我四处寻找了一段时间,没有找到好的资源来研究这个问题。

piok6c0g

piok6c0g1#

下面可能会给你一些基本的想法

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(<URI>);
HttpEntity entity = new FileEntity(file,ContentType.MULTIPART_FORM_DATA);
//there are other types of entities and content types too check documentation
req.setEntity(entity);
HttpResponse response = httpClient.execute(req);

相关问题