【发布时间】:2020-06-10 20:25:24
【问题描述】:
我将调用一个 POST REST API,它在其主体中接受一个文件。
如果我发送几个 KB 的文件(使用下面的代码),大约需要 300 毫秒。而且,如果我发送大约 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
【问题讨论】:
标签: java file file-upload inputstream