Отправить ByteArrayOutputStream с Джерси - PullRequest
0 голосов
/ 21 февраля 2019

Я пытаюсь отправить ZIP-файл ByteArrayOutputStream с POST, используя Джерси.

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: Не удалось определить границу из сообщения!

Моя подпись:

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

Когда я вызываю мой метод ws с Почтальоном, файл отправляется с успехом.

Что мне еще нужно сделать?

1 Ответ

0 голосов
/ 22 февраля 2019

Я смог сделать это, выполнив:

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>
...