gpt4 book ai didi

java - 使用 Spring 的 RestTemplate 将文件作为 block 发送

转载 作者:行者123 更新时间:2023-12-01 18:13:15 25 4
gpt4 key购买 nike

我要调用 POST REST API,该 API 接受其主体中的文件。

如果我发送几 KB 的文件(使用下面的代码),大约需要 300 毫秒。而且,如果我发送大约 5 GB 的文件,它会抛出内存不足异常。

是否有可能将文件作为 block 按顺序发送?目前,我们正在避免任何并行处理。

非常感谢代码或引用中的任何帮助。

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

最佳答案

你可以试试这个方法

@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找到了这个引用资料。希望对您有所帮助。

关于java - 使用 Spring 的 RestTemplate 将文件作为 block 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60422807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com