Konstantin Panisov
Konstantin Panisov

Reputation: 1

How to upload a big file by RestTemplate?

I get a MultipartFile from users and resent it to an API by RestTemplate. But I get ArrayIndexOutOfBoundsException if a file size biger than 2gb


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]

Method for sending request:

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();

    }

I also write LoggingInterceptor which do nothing, i needed to check the headers, but if i delete this. I get "501 chunked transfer encoding not supported" I don't know why it happend.

  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;
    }

I tried to find info on web, but I didn't find any way how to do that.

Upvotes: 0

Views: 54

Answers (2)

Hasim Abdul Halim
Hasim Abdul Halim

Reputation: 1

You can use HttpClient:

PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
    
    CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(poolingConnectionManager)
                .build();

RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));

Upvotes: 0

Priyam Kumari
Priyam Kumari

Reputation: 1

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();
  }

}

Upvotes: -4

Related Questions