title: How do I receive a file upload in spring mvc using both multipart/form and chunked encoding?
body: <p>I am trying to write a spring mvc method that can receive either a multipart/form or transfer-encoding chunked file upload.  I can write a separate method to handle each type but I'd like to do it with the same method so i can use the same REST POST uri such as: </p>
  
  <pre><code>http://host:8084/attachments/testupload
  </code></pre>
  
  <p>Here is my best attempt so far: </p>
  
  <pre><code>@RequestMapping(value = { "/testupload" }, method = RequestMethod.POST, produces = 
    "application/json")
  public @ResponseBody
  ResponseEntity&lt;MessageResponseModel&gt; testUpload(
    @RequestParam(value = "filedata", required = false) MultipartFile filedata,
    final HttpServletRequest request) throws IOException {
  
    InputStream is = null;
    if (filedata == null) {
      is = request.getInputStream();
    }
    else {
      is = filedata.getInputStream();
    }
    byte[] bytes = IOUtils.toByteArray(is);
    System.out.println("read " + bytes.length + " bytes.");
  
    return new ResponseEntity&lt;MessageResponseModel&gt;(null, null, HttpStatus.OK);
  }
  </code></pre>
  
  <p>Using the above method I can upload a multipart file, but if i upload a chunked file I get an exception from spring that says: </p>
  
  <pre><code>org.springframework.web.multipart.MultipartException: \
  The current request is not a multipart request
  </code></pre>
  
  <p>If I remove the MultipartFile request param it works great for transfer encoding chunked.  If I leave it in it works great for MultipartFile uploads.  How can I do it with the same method to handle both upload types? </p>
  
  <p>This works fine for chunked: </p>
  
  <pre><code>@RequestMapping(value = { "/testupload" }, method = RequestMethod.POST, produces = 
    "application/json")
  public @ResponseBody
  ResponseEntity&lt;MessageResponseModel&gt; testUpload(
    final HttpServletRequest request) throws IOException {
  
    InputStream is = null;
    is = request.getInputStream();
    byte[] bytes = IOUtils.toByteArray(is);
    System.out.println("read " + bytes.length + " bytes.");
  
    return new ResponseEntity&lt;MessageResponseModel&gt;(null, null, HttpStatus.OK);
  }
  </code></pre>
  
  <p>and this works great for MultipartFile: </p>
  
  <pre><code>@RequestMapping(value = { "/testupload" }, method = RequestMethod.POST, produces = 
    "application/json")
  public @ResponseBody
  ResponseEntity&lt;MessageResponseModel&gt; testUpload(
    @RequestParam MultipartFile filedata) throws IOException {
  
    InputStream is = null;
    is = filedata.getInputStream();
    byte[] bytes = IOUtils.toByteArray(is);
    System.out.println("read " + bytes.length + " bytes.");
  
    return new ResponseEntity&lt;MessageResponseModel&gt;(null, null, HttpStatus.OK);
  }
  </code></pre>
  
  <p>It should be possible, does anybody know how to do this? </p>
  
  <p>Thank you,
  Steve</p>