InputStream / RestTemplate永久复制文件

时间:2019-07-12 13:29:06

标签: java android spring

我有这段代码块,可以使用InputStream将文件复制到远程服务器:

    final InputStream fis = new FileInputStream(new File(CACHED_CLONE_FILE_NAME));
    final RequestCallback requestCallback = new RequestCallback()
    {
        public void doWithRequest(final ClientHttpRequest request)
        {
            try {
                request.getHeaders().add("Content-type", "application/octet-stream");

                OutputStream procOutput =  request.getBody();
                BufferedOutputStream bos = new BufferedOutputStream(procOutput);
                IOUtils.copy(fis, bos); 
            }
            catch (IOException ex) { 
            }
        }
    };

    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setBufferRequestBody(false);
    restTemplate.setRequestFactory(requestFactory);
    final HttpMessageConverterExtractor<String> responseExtractor = new HttpMessageConverterExtractor<String>(String.class, restTemplate.getMessageConverters());
    restTemplate.execute(urlToPost, HttpMethod.POST, requestCallback, responseExtractor);

它可以工作,但是对于一个小文件(4MB)来说,复制它要花费数小时。为什么会发生这种情况,我该怎么做?

此代码正在Android设备上使用,并且没有网络问题。

0 个答案:

没有答案