How to forward large files with RestTemplate?

前端 未结 3 1199
再見小時候
再見小時候 2020-11-29 01:38

I have a web service call through which zip files can be uploaded. The files are then forwarded to another service for storage, unzipping, etc. For now the file is stored on

3条回答
  •  星月不相逢
    2020-11-29 02:33

    The only part of @artbristol's answer you really need is this (which you can set up as a RestTemplate Spring bean):

    final RestTemplate restTemplate = new RestTemplate();
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setBufferRequestBody(false);     
    restTemplate.setRequestFactory(requestFactory);     
    

    After that, I think just using a FileSystemResource as your request body will do the right thing.

    I've also used an InputStreamResource successfully this way, for cases where you already have the data as an InputStream and don't need to consume it multiple times.

    In my case, we had gzipped our files and wrapped a GZipInputStream in an InputStreamResource.

提交回复
热议问题