title: Stream ending unexpectedly when doing a multipart form-data request
body: <p>So we have a server app written in java and it has a rest service to which you can upload files in the form of a multipart request. This is the signature of the method</p>
  
  <pre><code>@POST
  @Transactional(TxType.REQUIRED)
  @Path("/{id}")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public ContentInfo uploadToInstanceFromMultipart(UploadRequest uploadRequest, @PathParam(JsonKeys.ID) String id) {
  </code></pre>
  
  <p>As you can see thre is nothing special about it, just a post service. However on the other side we are writing an ms office plugin (VSTO) that sends a file to that service using RestSharp.</p>
  
  <pre><code>  void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
      {
          var client = new RestClient("...");
          client.Authenticator = new HttpBasicAuthenticator(...);
          var request = new RestRequest("api/content/{id}", Method.POST);
          var name = Doc.Name;
          var contentId = getContentId(name);
          var fileName = getName(name);
  
          request.AddUrlSegment("id", contentId); 
          request.AddFile(fileN, Doc.Path + "\\" + Doc.Name, "application/msword");
  
          IRestResponse response = client.Execute(request);
          var content = response.Content;
          MessageBox.Show(content);
  </code></pre>
  
  <p>And i don't see anything special in here too but when saving the file (thus sending it to the server), the server responds with the following exception</p>
  
  <pre><code>(default task-33) Generic exception: javax.ws.rs.WebApplicationException: Processing of multipart/form-data request failed. Stream ended unexpectedly
  </code></pre>
  
  <p>And boils down to</p>
  
  <pre><code>Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
  at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:1005)
  at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:903)
  at java.io.FilterInputStream.read(FilterInputStream.java:133)
  at org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:134)
  at java.io.FilterInputStream.read(FilterInputStream.java:107)
  at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
  at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
  at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:347)
  ... 70 more
  </code></pre>
  
  <p>Any ideas what is going on with the request?</p>