将文件作为块发送

问题描述 投票:0回答:1

我将调用POST REST API,该API接受其主体中的文件。

如果我发送几个KB的文件(使用下面的代码),则大约需要300毫秒。而且,如果我发送大约5 GB的文件,则会抛出内存不足异常。

是否有可能按顺序将文件作为块发送?目前,我们正在避免任何并行处理。

高度赞赏在代码或参考方面的任何帮助。

public static Resource getTestFile() throws IOException {
    Path testFile = Paths.get(FILE_PATH);
    System.out.println("Creating and Uploading Test File: " + testFile);
    return new FileSystemResource(testFile.toFile());
}
private static void uploadSingleFile() throws IOException {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", getTestFile());

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

    String serverUrl = "http://localhost:8080/place";

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<DataTransportResponse> response = restTemplate.postForEntity(serverUrl, requestEntity,
                    DataTransportResponse.class);

    System.out.println("Response code: " + response.getStatusCode());
}
java file file-upload inputstream
1个回答
0
投票

您已经有了正确的关键字“ Chunked Upload”。该线程中有一些解决方案(How could I upload a large file in chunks using Java?),但是您可能必须重写整个方法。

© www.soinside.com 2019 - 2024. All rights reserved.