2

I'm trying to send file on my rest service. I'm using apache httpcomponents 4.3. It works, but it use about 600 MB. Always, if file 200 KB or 15 GB it use 600 MB of Ram.

If I remove addPart - memory is OK.

So, why file sending get so much memory?

This is my code

           HttpClientBuilder clientBuilder = HttpClientBuilder.create();

            CloseableHttpClient client = clientBuilder.build();

            HttpPost post = new HttpPost(url);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            builder.addTextBody("jsonData", gson.toJson(dto));
            builder.addPart("file", new FileBody(file, ContentType.APPLICATION_OCTET_STREAM));

            post.setEntity(builder.build());
            HttpResponse response = client.execute(post);
Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
Andrew
  • 399
  • 5
  • 17

1 Answers1

1

For an older apache httpcomponents, there is a reported issue of HttpMultipartMode.BROWSER_COMPATIBLE not working. It appears it wasn't fixed.

Try changing it to:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("jsonData", gson.toJson(dto));
builder.addPart("file", new FileBody(file));