簡體   English   中英

如何使用Spring REST分段發送大文件(REST客戶端)

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

我正在使用Spring REST編寫一個客戶端,該客戶端會將文件上傳到DB。 以下是我無法更改的服務器端控制器代碼:

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

我發送文件的客戶端代碼是:

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

但是,當我使用此代碼發送例如50 MB的文件時,它會引發“ 413請求實體太大錯誤”

有人可以幫我解決如何分塊發送大文件嗎?

謝謝與問候,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;
}

更新資料

好的,因此您已經設置了multipartMaxFileSize,但與此同時,如果單個文件大於10MB,則還需要設置最大請求大小

似乎您正在使用Spring 4.x

所以配置就像

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

官方資料

說明:默認情況下,SimpleClientHttpRequestFactory在內部緩沖請求正文。

弄虛作假

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

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM