Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
@Stacks28 Is this possible to do using Spring instead of HTTPURLConnection? – RSenApps 10 mins ago
i hvnt worked with spring buddy so could not suggest you – Stacks28 6 mins ago
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.