我正在尝试通过REST API向mft服务器发送一些消息,并且我正在使用MultipartHttpEntityBuilder
构建消息,但是与原始消息一起,一些不需要的标头和其他数据也已附加。我发现了类似的问题
MultipartEntityBuilder: Omit Content-Type and Content-Transfer,但这很有帮助。
我的代码段:
HttpPut putRequest = new HttpPut(MFTSERVER_REST_LINK);
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("something","<head><content>xyz</content></head>".getBytes(), ContentType.APPLICATION_XML,"fileName");
HttpEntity httpEntity = builder.build();
putRequest.setEntity(httpEntity) ;
httpClient.execute(putRequest);
要写入文件的预期内容:
<head><content>xyz</content></head>
但是,实际上已写入文件:
--p13fxV0SO5Y6zSxYnGPJlfGPgX8snL
Content-Disposition: form-data; name="something"; filename="fileName"
Content-Type: application/xml; charset=ISO-8859-1
<head><content>xyz</content></head>
--p13fxV0SO5Y6zSxYnGPJlfGPgX8snL--
有人可以帮我解决这个问题吗?
AbstractMultipartForm
您会看到,要写入输出流的元素的列表依次是边界,标头,正文,然后依次是边界。因此,如果您想用字节写一些内容。然后,您应该使用 void doWriteTo(
final OutputStream out,
final boolean writeContent) throws IOException {
final ByteArrayBuffer boundary = encode(this.charset, getBoundary());
for (final FormBodyPart part: getBodyParts()) {
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(CR_LF, out);
formatMultipartHeader(part, out);
writeBytes(CR_LF, out);
if (writeContent) {
part.getBody().writeTo(out);
}
writeBytes(CR_LF, out);
}
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(TWO_DASHES, out);
writeBytes(CR_LF, out);
}
。
ByteArrayEntity