java 413 请求实体太大 - 使用 Spring Boot 和 Rest 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34762394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:17:56  来源:igfitidea点击:

413 Request Entity Too Large - With Spring Boot and Rest Template

javaangularjsspringamazon-web-servicesresttemplate

提问by aknon

Using Spring Rest Template to upload a 100 MB file, using a multipart post request.

使用 Spring Rest 模板上传 100 MB 文件,使用多部分发布请求。

Client Code:

客户代码:

HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

    map.add("name", chunk.getFilename());
    map.add("filename", chunk.getFilename());
    map.add("flowChunkNumber", chunk.getNumber());
    map.add("flowChunkSize", chunkSize);
    map.add("flowIdentifier", chunk.getIdentifier());
    map.add("flowTotalSize", chunk.getTotalSize());
    map.add("flowCurrentChunkSize", chunk.getSize());
    map.add("file", chunk.getResource() );

    HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(map, headers);


    ResponseEntity<String> response = executeForResponse(
            baseUri().path("/api/v1/uploads/chunks")
                                        .build().toUri(), HttpMethod.POST, entity, String.class);

However the server ( Springs Boot application deployed on Amazon AWS ) returns :

但是服务器(部署在 Amazon AWS 上的 Springs Boot 应用程序)返回:

org.springframework.web.client.HttpClientErrorException: 413 Request Entity Too Large
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:545)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:466)

Strangely enough I can upload the same file using an angular JS code to the same Spring Boot application.

奇怪的是,我可以使用 angular JS 代码将同一个文件上传到同一个 Spring Boot 应用程序。

Though in angular code I use : forceChunkSize : true

虽然在角度代码中我使用: forceChunkSize : true

Can I do the same in java ?

我可以在 java 中做同样的事情吗?

回答by Ali Dehghani

Maybe this helps:

也许这有帮助:

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

RestTemplate template = new RestTemplate(factory);

When sending large amounts of data via POST or PUT, it is recommended to change this property to false

通过POST或PUT发送大量数据时,建议将此属性改为false

回答by Musa

Add This content your configuration class of application.

添加此内容您的应用程序配置类。

@Bean

public MultipartConfigElement multipartConfigElement() {

     MultipartConfigFactory factory = new MultipartConfigFactory();

     factory.setMaxFileSize("200MB");

     factory.setMaxRequestSize("200MB");

     return factory.createMultipartConfig();

}