我正在尝试将HTTP Post Multipart请求发送到JAVA中的本地服务器 . 我正在尝试发送以下内容:
{
"content-disposition": "form-data; name=\"metadata\"",
"content-type": "application/x-dmas+json",
"body": JSON.stringify(client_req)
},
{
"content-disposition": "attachment; filename=\"" + file + "\"; name=\"file\"",
"content-type": "application/octet-stream",
"body": [file content]
}
我查看了Apache HTTP组件,但它不允许我指定每个部分的内容类型和处置 . 这是我使用Apache HTTP API在JAVA中编写的内容:
`CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost("IP");
FileBody bin = new FileBody(new File(args[0]), "application/octet-stream");
StringBody hash = new StringBody("{\"hash\": \"\", \"policy\": {\"retention_permitted\": true, \"distribution\": \"global\"}}", ContentType.create("application/x-dmas+json"));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("metadata", hash)
.addPart("file", bin)
.build();
httppost.setEntity(reqEntity);
`
2 回答
FilePart和StringPart的构造函数参数和方法,您使用它们组成构成多部分请求的Part [],提供此信息 .
可能为时已晚,但作为对同一问题的答案的任何人的参考,MultipartEntityBuilder类中有几种方法允许您为每个部分设置内容类型和内容处置 . 例如,
如果我们在您的示例中使用上述方法,