Java.io.IOException: Error writing to server

In my project, I am trying to upload a file to Amazon S3. Now the code can run, but SOMETIME gets an error and I can't see the file on S3. Log below:

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)

      

I don't understand why this is happening. Please take a look below and help me if you can. My code is below:

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<String, Vector<String>>();
    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", "<sessionId_of_sfdc_username>"); // 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() > 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<String> 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 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();
    }
}

      

And the error in the line int respCode = connection.getResponseCode();

+3


source to share





All Articles