title: Spring REST API with Multipart File Upload not working for optional parameter
body: <p>I'm using Spring framework, but not too familiar with it. I'm writing a REST API, specifically a POST method that handles a file upload where the file is optional.</p>
  
  <p>I've tried to simplify the method down but still having a problem. Here is what I have</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, value="/items")
  public String create(@RequestParam(value="file", required=false) MultipartFile file) {
      return "Create";
  }
  </code></pre>
  
  <p>The method works fine when I include a file in my form request, however when I don't have a file (as I want it to be optional). I have tried replacing the <code>@RequestParam</code> with a <code>HttpServletRequest</code> parameter yielding the same results.</p>
  
  <p>I get a 500 error with the following stack trace:</p>
  
  <pre><code>java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;root cause&lt;/b&gt; &lt;pre&gt;org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
      org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:99)
      org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.&amp;lt;init&amp;gt;(StandardMultipartHttpServletRequest.java:77)
      org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76)
      org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1067)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:906)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;root cause&lt;/b&gt; &lt;pre&gt;java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
      org.apache.catalina.connector.Request.parseParts(Request.java:2806)
      org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
      org.apache.catalina.connector.Request.getParameter(Request.java:1145)
      org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
      org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;root cause&lt;/b&gt; &lt;pre&gt;org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
      org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:311)
      org.apache.catalina.connector.Request.parseParts(Request.java:2737)
      org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
      org.apache.catalina.connector.Request.getParameter(Request.java:1145)
      org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
      org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;root cause&lt;/b&gt; &lt;pre&gt;org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
      org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:480)
      org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:889)
      org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.&amp;lt;init&amp;gt;(FileUploadBase.java:854)
      org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:259)
      org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:283)
      org.apache.catalina.connector.Request.parseParts(Request.java:2737)
      org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
      org.apache.catalina.connector.Request.getParameter(Request.java:1145)
      org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
      org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  </code></pre>
  
  <p>I found only one post in my search on the web and it talks about this potentially being a bug in Spring.  I've been stuck on this problem for a while now and would great appreciate some help. </p>
  
  <p>Thanks</p>