我将调用一个POST REST API,它接受主体中的一个文件。
如果我发送几KB的文件(使用下面的代码),它需要大约300ms。而且,如果我发送了大约5 GB的文件,它会抛出内存不足异常。
有没有可能以块的形式按顺序发送文件?在这个时间点,我们正在避免任何并行处理。
在代码或参考方面的任何帮助都是非常感谢的。
public static Resource getTestFile() throws IOException {
Path testFile = Paths.get(FILE_PATH);
System.out.println("Creating and Uploading Test File: " + testFile);
return new FileSystemResource(testFile.toFile());
}
private static void uploadSingleFile() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", getTestFile());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
String serverUrl = "http://localhost:8080/place";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<DataTransportResponse> response = restTemplate.postForEntity(serverUrl, requestEntity,
DataTransportResponse.class);
System.out.println("Response code: " + response.getStatusCode());
}
spring:
application:
name: file-manager
servlet:
multipart:
max-file-size: 20480MB
max-request-size: 2048MB
application:
seal:
id: 104912发布于 2020-03-05 00:30:36
我把问题解决了-
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());发布于 2020-02-27 22:37:07
你可以试试这种方式。
@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;
}我从这个链接post中找到了这个引用。希望这是有帮助的。
https://stackoverflow.com/questions/60422807
复制相似问题