gpt4 book ai didi

java - MultipartEntityBuilder 将 Content-Disposition header 添加到二进制文件并破坏它

转载 作者:行者123 更新时间:2023-12-05 07:57:32 24 4
gpt4 key购买 nike

我有一个 Java 代码片段,它通过网络将二进制文件发送到 REST 客户端,不幸的是,对二进制文件进行编码的 multipartEntityBuilder 添加了一些损坏文件的 header ,例如它添加了一个 Content-Disposition ,它不是存在于原始文件中。

我该如何解决这个问题?

CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(getRemoteUrlString() + "/webresources/file/upload");
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.STRICT);
multipartEntityBuilder.setBoundary("custyom-mimetype-boundary");
multipartEntityBuilder.addPart("fileName", (StringBody) valuesList.get(0));
multipartEntityBuilder.addPart("file", (FileBody) valuesList.get(1));
httpPost.setEntity(multipartEntityBuilder.build());
httpPost.setEntity(multipartEntityBuilder.build());
HttpResponse response = null;
try
{
response = httpclient.execute(httpPost);
}
catch (IOException ex)
{
}

最佳答案

我找到的唯一解决方案是将 header 留在发送端并修改接收端,显然,如果您不先读取 header 并丢弃它们,readBodyData 函数也会读取 header 。

//We read uselessHeaders otherwise readBodyData includes them and corrupts some files
String uselessHeaders = multipartStream.readHeaders();
multipartStream.readBodyData(byteArrayOutputStream);

关于java - MultipartEntityBuilder 将 Content-Disposition header 添加到二进制文件并破坏它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com