0

I'm trying to send a ByteArrayOutputStream zip file with POST using Jersey.

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

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

But on server side I'm receiving:

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

My pom:

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

When I call my ws method with Postman the file is send with success.

What more I have to do?

aseolin
  • 1,184
  • 3
  • 17
  • 35
  • You need to send a multipart request. Setting the mediatype is not enough. See [this post](https://stackoverflow.com/q/28754766/2587435) and search for other examples. – Paul Samsotha Feb 22 '19 at 06:18

1 Answers1

0

I was able to do that by doing:

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();
        }
    }

And I had to add the following dependecies:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5</version>
</dependency>
aseolin
  • 1,184
  • 3
  • 17
  • 35