title: HttpsURLConnection POST request of a large file throws IOException after several minutes
body: <p>I've been working on this bug for several days now and couldn't solve it.</p>
  
  <p>I wrote an HttpsURLConnection client to upload large files(>1GB) using POST requests.
  I also implemented the server side using com.sun.net.httpserver.HttpServer.
  As the files are quite big I have to use the: setFixedLengthStreamingMode/setChunkedStreamingMode settings on my connection
  (bug is reproduced using either).
  Please notice I'm using an HTTPS connection for the upload as well.</p>
  
  <p>I'm uploading the file to several servers simultaneously (seperate thread for each http client, connected to a different server).
  I have set a limit on the concurrent uploads so each time only X threads have an open UrlConnection (bug was reproduced with X=[1..4]).
  (The other threads wait on a semaphore)</p>
  
  <p>My problem is such:
  When uploads takes less than 5 minutes (less than 4:50 minutes to be accurate) everything works just fine.
  If the first batch of threads takes more then 5 minutes to finish then an Exception is thrown for every active thread:</p>
  
  <p>java.io.IOException: Error writing request body to server
      at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(Unknown Source)</p>
  
  <p>The exception is thrown while trying to write to the HttpURLConnection output stream.
  [outputStream.write(buffer,0,len);]</p>
  
  <p>The next batch of threads will work just fine (even if they take more then 5
  minutes).
  Please notice that the servers are completely identical, and now the process will not fail thus leading me to think that the problem is not on the server side.
  (If it was then the second batch was suppose to fail after 5 minutes as well...)</p>
  
  <p>I have reproduced this issue with/without connect/read timeouts on the connection.
  Furthermore, on the server side I've seen the file is being created and growing until the exception occurs.
  About 20-40 seconds after the client throws an exception the server will throw an IOException "read timeout".</p>
  
  <p>I have collected a TCP/IP sample using wireshark and saw that the server sends me a FIN packet at about the time of the client exception, I have no idea why.
  (All connection seems functioning prior to that)</p>
  
  <p>I have read many threads on similiar issues but couldn't find any proper solution.
  (including <a href="https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests">Using java.net.URLConnection to fire and handle HTTP requests</a>)</p>
  
  <p>Any ideas on why is this happening?
  How can I find the cause of it?
  How can I solve it?</p>
  
  <p>Many Thanks.</p>
  
  <p>P.S
  I didn't publish the code because it is pretty long...
  But if it could help understanding my problem I will be glad to do so.</p>