简体   繁体   English

上传文件时如何从HttpEntity中删除请求头

[英]How to remove request header from HttpEntity while uploading file

I am trying to send some message to mft server via REST API, and I'm using MultipartHttpEntityBuilder to build the message but along with original message some unwanted header and additional data is also getting attached.我正在尝试通过 REST API 向 mft 服务器发送一些消息,并且我正在使用MultipartHttpEntityBuilder来构建消息,但与原始消息一起,一些不需要的标头和其他数据也被附加了。 I found similar issue MultipartEntityBuilder: Omit Content-Type and Content-Transfer , but it was helpful.我发现了类似的问题MultipartEntityBuilder: Omit Content-Type 和 Content-Transfer ,但它很有帮助。

My Code snippet :我的代码片段:

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);

Expected content to be written to file :要写入文件的预期内容:

<head><content>xyz</content></head>

But, actually written to file :但是,实际上写入文件:

--p13fxV0SO5Y6zSxYnGPJlfGPgX8snL
Content-Disposition: form-data; name="something"; filename="fileName"
Content-Type: application/xml; charset=ISO-8859-1

<head><content>xyz</content></head>
--p13fxV0SO5Y6zSxYnGPJlfGPgX8snL--

Can someone help me to solve this issue ?有人可以帮我解决这个问题吗?

If you are going to write Byte data in your post body.如果您要在帖子正文中写入Byte数据。 Then, you should use ByteArrayEntity instead of MultipartEntityBuilder .然后,您应该使用ByteArrayEntity而不是MultipartEntityBuilder Because, with doWriteTo method in AbstractMultipartForm .因为,在AbstractMultipartForm使用doWriteTo方法。 There is no way you can remove or skip unwanted header to be written to file.您无法删除或跳过要写入文件的不需要的标头。

 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);
    }

You could see, the list of elements which ware getting written to outputstream are boundary, header, body and then boundary again in sequence.您可以看到,写入输出流的元素列表依次是边界、标题、正文和边界。 So, if you wanted to write some content with bytes.所以,如果你想用字节写一些内容。 Then, you should use ByteArrayEntity .然后,您应该使用ByteArrayEntity

byte[] b = "This is hello".getBytes("UTF-8");
putRequest.setEntity(new ByteArrayEntity(b));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM