使用 Jersey 发送 ByteArrayOutputStream

Send ByteArrayOutputStream with Jersey

我正在尝试使用 Jersey POST 发送一个 ByteArrayOutputStream zip 文件。

Client client = Client.create();
client.resource(url);

ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, myBaosObject.toByteArray());

但在服务器端我收到:

WARN org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper - javax.ws.rs.WebApplicationException: org.apache.cxf.interceptor.Fault: Couldn't determine the boundary from the message!

我的pom:

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.9.1</version>
 </dependency>

当我用 Postman 调用我的 ws 方法时,文件发送成功。

我还需要做什么?

我可以通过以下方式做到这一点:

File file = null;
    try {
        // Transform baos into file
        InputStream is = new ByteArrayInputStream(baos.toByteArray());

        file = File.createTempFile("file ", "zip");
        FileUtils.copyInputStreamToFile(is, file);

        HttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        // Send file as part of body
        FileBody uploadFilePart = new FileBody(file);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("file", uploadFilePart);
        httpPost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httpPost);
        return response.toString();

    } finally {
        if (file != null) {
            file.delete();
        }
    }

而且我必须添加以下依赖项:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5</version>
</dependency>