Java 如何使用SpringREST多部分分块发送大文件(REST客户端)
我正在使用SpringREST编写一个客户端,它将文件上传到DB。 以下是我无法更改的服务器端控制器代码:Java 如何使用SpringREST多部分分块发送大文件(REST客户端),java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我正在使用SpringREST编写一个客户端,它将文件上传到DB。 以下是我无法更改的服务器端控制器代码: @RequestMapping(method = RequestMethod.POST) public ResponseEntity<UploadResponseDto> uploadFile(@RequestParam("file") MultipartFile file) throws IOException { String contentType = fil
@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);
}
@RequestMapping(method=RequestMethod.POST)
公共响应性上载文件(@RequestParam(“文件”)MultipartFile文件)引发IOException{
字符串contentType=file.getContentType();
if(contentType==null | |!contentType.equalsIgnoreCase(应用程序八位字节流)){
contentType=应用程序\八位字节\流;
}
GridFSFile GridFSFile=gridFsTemplate.store(file.getInputStream(),file.getOriginalFilename(),contentType);
多值映射头=新的LinkedMultiValueMap();
字符串fileLocation=linkTo(FileAttachmentController.class).slash(gridFSFile.getId()).toUri().toString();
headers.add(位置、文件位置);
UploadResponseDto UploadResponseDto=新的UploadResponseDto(file.getOriginalFilename(),fileLocation);
返回新的ResponseEntity(uploadResponseDto、Header、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);
SimpleClientHttpRequestFactory工厂=新的SimpleClientHttpRequestFactory();
factory.SetBufferreRequestBody(假);
RestTemplate RestTemplate=新的RestTemplate(工厂);
HttpHeaders=新的HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION,“承载者”+令牌);
headers.set(“接受”、“应用程序/json”);
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
File File=新文件(fileToUpload);
多值映射数据=新的LinkedMultiValueMap();
ByteArrayResource资源=新建ByteArrayResource(
Files.readAllBytes(path.get(fileToUpload))){
@凌驾
公共字符串getFilename(){
返回文件.getName();
}
};
数据。添加(“文件”,资源);
HttpEntity requestEntity=新HttpEntity(
数据、标题);
ResponseEntity apiResponse=null;
apiResponse=restTemplate.exchange(
“http://{end\u point\u url}”,
HttpMethod.POST、requestEntity、Map.class);
但当我使用此代码发送(比如50MB文件)时,它抛出“413请求实体太大错误”
有人能帮我把一个大文件分块发送吗
谢谢和问候,
Vikas Gite您可以使用
org.springframework.web.multipart.commons.commons多部分解析器
@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的文件,那么您还需要设置最大请求大小 似乎您正在使用Spring4.x 所以配置是这样的
spring.http.multipart.maxFileSize
spring.http.multipart.maxRequestSize
去润滑:
默认情况下,SimpleClientHttpRequestFactory在内部缓冲请求主体
弄虚作假
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setBufferRequestBody(false);
将其添加到现有的bean创建文件或从WebMVCConfigureAdapter扩展的文件中,但它并没有解决我的问题。413是服务器抛出的。恐怕将大小更改为50MB也不能解决我的错误。我正在这里上传17MB的文件。我的代码正确吗?你怎么认为?它会分块发送吗?很酷@VikasGite你可能错过了spring配置中的一些东西,而没有看到完整的代码我怎么能告诉你什么?你的服务器能够一次接受多少块?这是spring boot?是的。我有multipartMaxFileSize属性。但是,即使设置为50,对于小于20MB的文件,我仍然会收到错误。可能是@VikasGite的重复。您是否能够使用Springtemplate将文件作为块发送?如果是,请告诉我代码示例?maxFileSize和maxRequestSize都已设置。对不起,我在之前的评论中只提到了一个。