Java 使用Spring';RestTemplate
我将调用一个POST REST API,它在其主体中接受一个文件 如果我发送几个KB的文件(使用下面的代码),大约需要300毫秒。如果我发送大约5 GB的文件,它会抛出内存异常 是否有一种可能的方法可以按顺序将文件作为块发送?目前,我们正在避免任何并行处理 非常感谢您在代码或参考方面的任何帮助Java 使用Spring';RestTemplate,java,file,file-upload,inputstream,Java,File,File Upload,Inputstream,我将调用一个POST REST API,它在其主体中接受一个文件 如果我发送几个KB的文件(使用下面的代码),大约需要300毫秒。如果我发送大约5 GB的文件,它会抛出内存异常 是否有一种可能的方法可以按顺序将文件作为块发送?目前,我们正在避免任何并行处理 非常感谢您在代码或参考方面的任何帮助 public static Resource getTestFile() throws IOException { Path testFile = Paths.get(FILE_PATH);
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
公共静态资源getTestFile()引发IOException{
路径testFile=Path.get(文件路径);
System.out.println(“创建和上传测试文件:“+testFile”);
返回新的文件系统资源(testFile.toFile());
}
私有静态void uploadSingleFile()引发IOException{
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
MultiValueMap body=新链接的MultiValueMap();
add(“file”,getTestFile());
HttpEntity requestEntity=新的HttpEntity(主体、标题);
字符串serverUrl=”http://localhost:8080/place";
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity response=restTemplate.postForEntity(服务器URL、请求实体、,
DataTransportResponse.class);
System.out.println(“响应代码:+Response.getStatusCode());
}
春天:
应用程序:
名称:文件管理器
servlet:
多部分:
最大文件大小:20480MB
最大请求大小:2048MB
应用程序:
印章:
身份证号码: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;
}
我从这个链接中找到了这个参考。希望对你有帮助。我已经解决了-
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
MultiValueMap fileMap=新链接的MultiValueMap();
add(HttpHeaders.CONTENT_DISPOSITION,contentDisposition.toString());