【问题标题】:post file with httpcomponents. Something wrong with memory使用 httpcomponents 发布文件。记忆有问题
【发布时间】:2016-07-04 16:25:12
【问题描述】:

我正在尝试在我的休息服务上发送文件。我正在使用apache httpcomponents 4.3. 它有效,但它使用大约600 MB。总是,如果文件 200 KB15 GB 它使用 RAM 的 600 MB

如果我删除 addPart - 内存正常。

那么,为什么文件发送会占用这么多内存?

这是我的代码

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

【问题讨论】:

    标签: java http httpclient multipartentity


    【解决方案1】:

    对于较旧的 apache httpcomponents,报告了 issueHttpMultipartMode.BROWSER_COMPATIBLE 无法正常工作。它似乎没有修复。

    尝试将其更改为:

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

    【讨论】:

    • 感谢您的回答。我已经尝试过了,但它并没有解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2019-06-13
    • 2011-02-03
    • 1970-01-01
    • 2013-08-14
    • 2013-01-05
    相关资源
    最近更新 更多