使用httpcomponents发布文件。记忆有问题

时间:2016-07-04 16:25:13

标签: java http httpclient multipartentity

我正在尝试在我的休息服务上发送文件。我正在使用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);

1 个答案:

答案 0 :(得分:1)

对于较旧的apache httpcomponents,报告的HttpMultipartMode.BROWSER_COMPATIBLE MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("jsonData", gson.toJson(dto)); builder.addPart("file", new FileBody(file)); 无法正常工作。它似乎没有修复。

尝试将其更改为:

static int runSim(int thePeople, int theCount) {

    int count = 0;

    // Runs the Sim by Count
    for(int i = 1; i <= theCount; i++) {

        List<Integer> listOfGenNums = new ArrayList<>();

        Random rand = new Random();
        rand.setSeed(i);

     ppl: for (int j = 0; j <= thePeople; j++) {

            int genN = rand.nextInt(365);

            // Add Values to Arraylist
            listOfGenNums.add(j, genN);

            // Converted ArrayList to Array
            Object[] array = listOfGenNums.toArray();

            // Check Item by Item
            for(int h = 0; h <= array.length; h++) {
                for(int k = i+1; k <= array.length-2; k++) {

                    // Checks if Index[i] is Same as Index[j]
                    if (array[h].equals(array[k])) {

                        count += 1;
                        break ppl;

                    } // End IF

               } // End Inner Loop

            } // End Loop

        } // End People Loop

    } // End Simulation Loop

    return count;
}