Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Spring File Upload OutofMemory
    text
    copied!<p>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 <code>OutOfMemoryError</code>. 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.</p> <pre><code> 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&lt;String, Object&gt; parts = new LinkedMultiValueMap&lt;String, Object&gt;(); 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&lt;Object&gt; request = new HttpEntity&lt;Object&gt;(parts, header); restTemplate.postForLocation(url, request); Log.e("f", "finished"); return null; </code></pre> <p>Error: <code>dalvikvm-heap: Out of memory on a 67102090-byte allocation.</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload