5

I am using spring REST to write a client which will upload a file to DB. Following is the server side controller code which I can not change :

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<UploadResponseDto> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {

    String contentType =  file.getContentType();
    if ( contentType == null || !contentType.equalsIgnoreCase(APPLICATION_OCTET_STREAM)) {
        contentType = APPLICATION_OCTET_STREAM;
    }
    GridFSFile gridFSFile = gridFsTemplate.store(file.getInputStream(), file.getOriginalFilename(), contentType);

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    String fileLocation = linkTo(FileAttachmentController.class).slash(gridFSFile.getId()).toUri().toString();
    headers.add(LOCATION, fileLocation);
    UploadResponseDto uploadResponseDto = new UploadResponseDto(file.getOriginalFilename(), fileLocation);
    return new ResponseEntity<>(uploadResponseDto, headers, HttpStatus.CREATED);
}

And my client side code for sending file is :

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setBufferRequestBody(false);
    RestTemplate restTemplate = new RestTemplate(factory);

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + token);
    headers.set("Accept", "application/json");
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    File file = new File(fileToUpload);
    MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
    ByteArrayResource resource = new ByteArrayResource(
        Files.readAllBytes(Paths.get(fileToUpload))) {
        @Override
        public String getFilename() {
            return file.getName();
        }
    };
    data.add("file", resource);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(
        data, headers);
    ResponseEntity<Map> apiResponse = null;

    apiResponse = restTemplate.exchange(
        "http://{end_point_url}",
        HttpMethod.POST, requestEntity, Map.class);

But when I use this code to send lets say 50 MB file, it throws "413 Request entity too large error"

Can somebody please help me out on how to send a large file in chunks?

Thanks & Regards, Vikas Gite

4
  • your server is capable of accepting how much chunk at a time?this is spring boot? Commented Oct 4, 2017 at 7:07
  • Yes. I have property multipartMaxFileSize. But even though it is set to 50, I am still getting error for files less than 20MB size. Commented Oct 4, 2017 at 7:14
  • Possible duplicate of Increase HTTP Post maxPostSize in Spring Boot Commented Oct 4, 2017 at 9:13
  • @VikasGite Were you able to send the file as chunks using Springtemplate? If yes, kindly point me to the code examples? Commented Feb 26, 2020 at 23:20

2 Answers 2

1

You can specify a size of the upload file by using

org.springframework.web.multipart.commons.CommonsMultipartResolver

@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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

add this in your existing bean creation file or a file which extends from WebMvcConfigurerAdapter
Still it did not solved my problem. 413 is thrown by the server.
I am afraid, changing the size to 50MB has also not solved my error. I am trying to upload 17MB file here.Is my code correct? What do you think? Will it send in chunks?
cool @VikasGite may be you missed something in a spring configuration and without seeing the full code how I can able to tell anything?
0

Update

Okay so you have set multipartMaxFileSize, but along with this you also need to set max request size if you have a single file that's greater than 10MB

Seems you are using Spring 4.x

So config goes like

spring.http.multipart.maxFileSize
spring.http.multipart.maxRequestSize

Official Source

Depricated: By default SimpleClientHttpRequestFactory buffers the request body internally.

Make it false

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setBufferRequestBody(false);

Source

1 Comment

Both maxFileSize and maxRequestSize are already set. Sorry I mentioned only one in previous comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.