java - apache cxf MultiPart 请求没有 Content-Length 标头
问题描述
我有以下代码来发送多部分/表单数据请求。
List<Attachment> multipartData = new ArrayList<>();
ContentDisposition cd1 = new ContentDisposition("form-data; name=\"file\";
filename="+fileObj.getName());
FileInputStream inputStream = new FileInputStream(fileObj);
multipartData.add(new Attachment("file",inputStream, cd1));
MultipartBody multipart = new MultipartBody(multipartData);
在我的 RestClient 类中,我使用以下代码行使用 JAX-RS 客户端对象发送 POST 请求
if ("POST".equals(method)) {
response = this.client.getBuilder().post(Entity.entity(entity,MediaType.MULTIPART_FORM_DATA));
我使用 Wiremock 检查了 HTTP 请求正文,如下所示:
Transfer-Encoding: [chunked]
Accept: [*/*]
Cache-Control: [no-cache]
User-Agent: [Apache-CXF/3.2.5]
Connection: [keep-alive]
Host: [127.0.0.1:9990]
Pragma: [no-cache]
Content-Type: [multipart/form-data; boundary="uuid:04b491f5-50de-4f4f-b7c0-cd745136d3d1"]
--uuid:04b491f5-50de-4f4f-b7c0-cd745136d3d1
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <file>
Content-Disposition: form-data; name="file"; filename=sample.txt
<File content goes here>
我想知道请求有效负载中如何缺少内容长度标头。有没有办法将内容长度标头设置为请求?
请帮我。
解决方案
我使用 apache cxf WebClient 将传输编码取消设置为分块。
if ("POST".equals(method)) {
Invocation.Builder builder = this.client.getBuilder();
WebClient.getConfig(builder).getHttpConduit().getClient().setAllowChunking(false);
response = builder.post(Entity.entity(entity,MediaType.MULTIPART_FORM_DATA));
}
这样,客户端就可以发送带有内容长度标头的请求。
推荐阅读
- django - 出现 Django 错误(models.E028)。从桌面文件夹移动到 Ubuntu 服务器上的 var/www 文件夹后,适用于 6 种不同的模型
- gitpython - 如何获得提交的分阶段差异?
- c# - 为什么来自同一个客户端的调用不会同时执行
- html - 如何在 for 循环中使用 ngbDatepicker
- java - 如果在迭代之前调用了 remove 方法,则不会抛出 java List 并发修改异常
- docker - 作为构建代理的 Docker 容器出现错误“不支持针对正在运行的 Hyper-V 容器的文件系统操作”
- jquery - 开玩笑:TypeError:$(...).modal 不是函数
- javascript - 获取数据后解构数据导致错误
- python-3.x - 尝试初始化数据框列时,多处理代码不起作用
- plot - 从表中绘制 pyspark