Android的春天文件上传出现OutofMemory [英] Android Spring File Upload OutofMemory

查看:186
本文介绍了Android的春天文件上传出现OutofMemory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用下面的code将文件上载到一个REST API。这code正常工作的文件高达约20MB,但更大的文件会给出一个的OutOfMemoryError 。我想块的请求,并使用一个多形式的要求,这样我就不用在内存中保存整个文件,但也许是块太大?任何帮助是极大的AP preciated。

I am trying to use the following code to upload a file to a rest api. This code works fine for files up to around 20MB, but bigger files will give an OutOfMemoryError. I am trying to chunk the request and use a multipart-form request so that I don't have to keep the whole file in memory, but maybe the chunks are too big? Any help is greatly appreciated.

    final File file = new File(filePath);
    HttpHeaders header = new HttpHeaders();
    header.add("x-haiku-auth", HaikuAPI.getAuthHeader());
    header.setContentType(MediaType.MULTIPART_FORM_DATA);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(
            "https://" + HaikuAPI.getDomain() + "/api/assignments")
            .pathSegment(assignmentID + "", "submit");

    URI url = builder.build().toUri();

    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();

    parts.add("message[subject]", "Assignment Completed");
    parts.add("message[body]", message);
    parts.add("message[assignment_id]", assignmentID + "");
    Uri uri = Uri.fromFile(file);
    InputStreamResource res = new InputStreamResource(context.getContentResolver().openInputStream(uri)) {
        @Override
        public String getFilename() throws IllegalStateException {
            return file.getName();
        }

        @Override
        public long contentLength() throws IOException {
            return file.length();
        }
    };
    parts.add("files[][file]", res);
    parts.add("files[][filename]", file.getName());
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setChunkSize(1024);
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
    HttpEntity<Object> request = new HttpEntity<Object>(parts, header);
    restTemplate.postForLocation(url, request);
    Log.e("f", "finished");
    return null;

错误: dalvikvm堆:内存不足。在67102090字节分配

推荐答案

在分析code,似乎春天为Android缓冲的数据发送之前 - 这就是为什么你的监督办。还有一招然而(但到目前为止,我已经使人们有可能仅适用于上面的API 9级工作):您可以禁用缓冲其连接工厂,但只有在RestTemplate没有 ClientHtt prequestInterceptor 列表集(现在是多么的愚蠢吗?) - 幸运的你,你没有设置之一。所以,你的情况的情况很简单,只需要调用 factory.setBufferRequestBody(假); 您已经实例化后

Analyzing the code, it seems Spring for Android buffers its data before sending it - that's why your OOE. There is a trick however (but so far I've made it possible to work only for API level 9 above): you can disable the buffering for its connection factory but ONLY if the RestTemplate has no ClientHttpRequestInterceptor list set (now how stupid is that ?) - lucky you as you didn't set one. So in your case the situation is very simple, just call factory.setBufferRequestBody(false); after you've instantiated it.

这篇关于Android的春天文件上传出现OutofMemory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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