How to send large file using Spring REST multipart in chunks (REST client)

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


您可以使用指定上传文件的大小

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

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

链接地址: http://www.djcxy.com/p/48742.html

上一篇: 如何在.Net中处理多达100k个HTTP连接

下一篇: 如何在块中使用Spring REST multipart发送大文件(REST客户端)