0

I am to invoke a POST REST API which accepts a file in its body.

If I send a few KB's file (using below code) it takes about 300 ms. And, if i send about 5 GB's of file it throws Out Of Memory Exception.

Is there a possible way to send the file as Chunks, sequentially? We are avoiding any parallel processing, at this point in time.

Any help in code or reference is highly appreciated.

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

spring:
  application:
    name: file-manager
  servlet:
    multipart:
      max-file-size: 20480MB
      max-request-size: 2048MB 
application:
  seal:
    id: 104912
TuneIt
  • 572
  • 2
  • 9
  • 20

2 Answers2

1

you can try this way

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(54525952); //...specify your size of file  (20971520 - 20 MB) (54525952 - 52 MB)
    return multipartResolver;
}

I have found this reference from this link post. hope it is help ful.

-1

I got it resolved through -

MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
            fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
TuneIt
  • 572
  • 2
  • 9
  • 20
  • This code doesn't relate to anything in your original question. Please post complete solution or at least something that is relatable to the original example – Drew Oct 15 '22 at 05:48