我有一段Java代码片段,它通过Web将二进制文件发送到REST客户端,遗憾的是编码二进制文件的multipartEntityBuilder添加了一些损坏文件的头文件,例如它添加了一个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)
{
}
答案 0 :(得分:0)
我发现只有解决方案是将标题保留在发送方并修改接收方,显然如果您没有先读取标题并丢弃它们,readBodyData函数也会读取标题。
//We read uselessHeaders otherwise readBodyData includes them and corrupts some files
String uselessHeaders = multipartStream.readHeaders();
multipartStream.readBodyData(byteArrayOutputStream);