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 OutOfMemory. 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.
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/50MB.zip");
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 + "");
parts.add("files[][file]", new FileSystemResource(file));
parts.add("files[][filename]", "50MB.zip");
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
HttpEntity<Object> request = new HttpEntity<Object>(parts, header);
restTemplate.postForLocation(url, request);
return null;
Error: dalvikvm-heap: Out of memory on a 67102090-byte allocation.