title: java.io.IOException: Error writing to server
body: <p>In my project I'm trying to send file to Amazon S3. The code can run now but SOMETIME an error is occur and I can't see the file on S3. The log below: </p>
  
  <pre><code>java.io.IOException: Error writing to server
  at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:625)
  at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:637)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1321)
  at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
  at com.datagravity.gdphonehome.sample.SendingBigDataLocal.sendRequest(SendingBigDataLocal.java:219)
  at com.datagravity.gdphonehome.sample.SendingBigDataLocal.doGet(SendingBigDataLocal.java:121)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>I can't see why it happen. Please take a look below and help me if could. My code is below:</p>
  
  <pre><code>public void sendRequest(String request) throws JSONException {
  
      msg = new StringBuffer();
      String message = null;
  
      // Add text message parameter
      appendBoudary("text-message", "Text Message");
      message = msg.toString();
  
      DataFileReader fileUpload;
      mapUploadFiles = new Hashtable&lt;String, Vector&lt;String&gt;&gt;();
      fileUpload = new DataFileReader("/home/varick/DataGravity_Proxy_Application/DGPhoneHome(vuong.tran).war");                                       
      try {
           //Add file to upload
          org.codehaus.jettison.json.JSONObject jsonHeader = new JSONObject();
          jsonHeader = writeJSONHeader();
          Vector file_name = new Vector();
          file_name.add(fileUpload.getBytes());
          file_name.add(appendBoudaryFileHeader(jsonHeader, "phonehome32M-Varick(Test)"));
          mapUploadFiles.put(jsonHeader.toString(), file_name);
  
      } catch (Exception ex1) {
          ex1.printStackTrace();
      }
  
      try {
          URL url = new URL(request);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setDoInput(true);
          connection.setDoOutput(true);
          //connection.setRequestProperty("sessionId", "&lt;sessionId_of_sfdc_username&gt;"); // put sessionId into header
          connection.setRequestProperty("sessionId", json.getString("SessionId")); // sessionId of username: chau.dinh@enclave.vn
          connection.setRequestProperty("SessionIdSFDC", json.getString("SessionIdSFDC"));            
  
          if (mapUploadFiles.size() &gt; 0) {
              connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + Constants.BOUNDARY_VALUE);
              connection.setRequestProperty("Connection", "Close");
          }
          connection.setRequestMethod("POST");
          out = connection.getOutputStream();
          // Send message first
          if (message != null) {
  
              // Send text parameters
              out.write(message.toString().getBytes());
              // Send files
              if (null != mapUploadFiles) {
                  Enumeration&lt;String&gt; e = mapUploadFiles.keys();
                  while (e.hasMoreElements()) {
                      String keyFile = (String) e.nextElement();
                      if (!keyFile.equals(FLAG_MUTI_SUPPORT)) {
                          Vector ItemFiles = (Vector) mapUploadFiles.get(keyFile);
                          //System.out.println("Vector Size: " + ItemFiles.size());
                          String fileHeader = (String) ItemFiles.elementAt(1);
                          System.out.println(fileHeader);
                          // Write header file
                          out.write(fileHeader.getBytes());
                          out.write("\r\n".getBytes());
                          // Write data file uploading
                          byte[] data = (byte[]) ItemFiles.elementAt(0);
                          out.write(data);
                      }
                      out.write("\r\n".getBytes());
                  }
              }
  
              // Send the end signal
              String endBoundary = "\r\n--" + Constants.BOUNDARY_VALUE + "--\r\n";
              out.write(endBoundary.getBytes());
              int respCode = connection.getResponseCode();                
              if (respCode == HttpURLConnection.HTTP_OK) {
                  // reads server's response
                  BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                  String response = reader.readLine();
                  System.out.println(response);
              } else {
                  System.out.println("Server returned non-OK code: " + respCode);
              }
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And the error at line <code>int respCode = connection.getResponseCode();</code></p>