我从用户那里获取了一个 MultipartFile,并通过 RestTemplate 将其重新发送到 API。但如果文件大小大于 2gb,我会得到 ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 2147468288 out of bounds for byte[1819689347]
at java.base/java.lang.System.arraycopy(Native Method) ~[na:na]
at org.springframework.util.FastByteArrayOutputStream.resize(FastByteArrayOutputStream.java:299) ~[spring-core-6.1.8.jar:6.1.8]
at org.springframework.util.FastByteArrayOutputStream.toByteArrayUnsafe(FastByteArrayOutputStream.java:215) ~[spring-core-6.1.8.jar:6.1.8]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:44) ~[spring-web-6.1.8.jar:6.1.8]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-6.1.8.jar:6.1.8]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:889) ~[spring-web-6.1.8.jar:6.1.8]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:790) ~[spring-web-6.1.8.jar:6.1.8]
发送请求的方法:
protected String postFileRestTemplate(String addToBaseURL,
InputStreamResource inputStreamResource) {
restTemplate.setInterceptors(Collections.singletonList(new LoggingInterceptor()));
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("content", "iso");
builder.part("filename", inputStreamResource);
MultiValueMap<String, HttpEntity<?>> build = builder.build();
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(build, httpHeaders);
ResponseEntity<String> exchange = restTemplate.postForEntity(
baseURL + addToBaseURL,
objectHttpEntity,
String.class);
log.info(exchange.getBody());
return exchange.getBody();
}
我还编写了 LoggingInterceptor,它什么也不做,我需要检查标题,但如果我删除它。我收到“501 不支持分块传输编码”我不知道为什么会发生。
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
return response;
}
我尝试在网上查找信息,但没有找到任何方法。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}