如何使用RestTemplate转发大文件? [英] How to forward large files with RestTemplate?

查看:1167
本文介绍了如何使用RestTemplate转发大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以上传zip文件的web服务调用。这些文件然后被转发到另一个服务进行存储,解压缩等。
现在文件存储在文件系统中,然后构建FileSystemResource。

 资源zipFile = new FileSystemResource(tempFile.getAbsolutePath()); 

为了节省时间,我可以使用ByteStreamResource(不需要在磁盘上保存文件之前转发),但为此,我需要建立一个字节数组。在大文件的情况下,我将得到一个OutOfMemory:java heap space的错误。


$ b

  ByteArrayResource r = new ByteArrayResource(inputStream。的getBytes()); 

转发文件的任何解决方案都不会使用RestTemplate发生OutOfMemory错误?

解决方案

您可以使用 execute 来进行这种低级操作。在这个片段中,我使用了Commons IO的 copy 方法来复制输入流。您需要自定义 HttpMessageConverterExtractor 作为您预期的响应类型。

  final InputStream fis = new FileInputStream(new File(c:\\autoexec.bat)); //或任何
RequestCallback requestCallback = new RequestCallback(){
@Override $ b $ public void doWithRequest(final ClientHttpRequest request)throws IOException {
request.getHeaders()。add( Content-type,application / octet-stream);
IOUtils.copy(fis,request.getBody());
}
};
final RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate.setRequestFactory(requestFactory);
final HttpMessageConverterExtractor< String> responseExtractor =
new HttpMessageConverterExtractor< String>(String.class,restTemplate.getMessageConverters());
restTemplate.execute(http:// localhost:4000,HttpMethod.POST,requestCallback,responseExtractor);

(感谢Baz指出你需要调用 setBufferRequestBody(false) 或者它会击败这一点)


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 the file system, then a FileSystemResource is built.

Resource zipFile = new FileSystemResource(tempFile.getAbsolutePath());

I could use a ByteStreamResource in order to save time(the saving of the file on disk is not needed before forwarding) but for that i need to build a byte array. In case of large files I will get an "OutOfMemory : java heap space" error.

ByteArrayResource r = new ByteArrayResource(inputStream.getBytes());

Any solutions to forwarding files without getting an OutOfMemory error using RestTemplate?

解决方案

You can use execute for this kind of low-level operation. In this snippet I've used Commons IO's copy method to copy the input stream. You would need to customize the HttpMessageConverterExtractor for the kind of response you're expecting.

final InputStream fis = new FileInputStream(new File("c:\\autoexec.bat")); // or whatever
final RequestCallback requestCallback = new RequestCallback() {
     @Override
    public void doWithRequest(final ClientHttpRequest request) throws IOException {
        request.getHeaders().add("Content-type", "application/octet-stream");
        IOUtils.copy(fis, request.getBody());
     }
};
final RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);     
restTemplate.setRequestFactory(requestFactory);     
final HttpMessageConverterExtractor<String> responseExtractor =
    new HttpMessageConverterExtractor<String>(String.class, restTemplate.getMessageConverters());
restTemplate.execute("http://localhost:4000", HttpMethod.POST, requestCallback, responseExtractor);

(Thanks to Baz for pointing out you need to call setBufferRequestBody(false) or it will defeat the point)

这篇关于如何使用RestTemplate转发大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆