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

编程入门 行业动态 更新时间:2024-10-14 20:22:19
本文介绍了上传文件时如何从HttpEntity中删除请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过REST API向mft服务器发送一些消息,并且我正在使用 MultipartHttpEntityBuilder 来构建消息,但是与原始消息一起,一些不需要的标头和其他数据也已被附加.我发现了类似的问题MultipartEntityBuilder:省略Content-Type和Content-Transfer ,但这很有帮助.

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. I found similar issue MultipartEntityBuilder: Omit Content-Type and Content-Transfer, but it was helpful.

我的代码段:

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 ?

推荐答案

如果您要在帖子正文中写入 Byte 数据.然后,您应该使用 ByteArrayEntity 而不是 MultipartEntityBuilder .因为使用 AbstractMultipartForm 中的httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java"rel =" nofollow noreferrer> doWriteTo 方法>.无法删除或跳过不需要的标头以将其写入文件.

If you are going to write Byte data in your post body. Then, you should use ByteArrayEntity instead of MultipartEntityBuilder. Because, with doWriteTo method in AbstractMultipartForm. 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); }

您会看到,要写入输出流的元素的列表依次是边界,标头,正文和边界.因此,如果您想用字节写一些内容.然后,您应该使用 ByteArrayEntity .

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.

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

更多推荐

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

本文发布于:2023-11-25 23:08:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传文件   HttpEntity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!