2

I am trying to upload a zip file to our server along with the params using UrlConnection. This is uploading the file but it is not sending the parameters.

Here is the code.

private static final String CRLF = "\r\n"; // Line separator required by multipart/form-data.
private static final String CHARSET_UTF_8 = "UTF-8";
private static final String TWO_HYPHENS = "--";


public static void uploadReport(String filePath) {
    File binaryFile = new File(filePath);
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
    String urlString = "https://mycompany.com/api/file/uad.json";

    HttpsURLConnection connection = null;
    DataOutputStream dataOutoutStream = null;
    try {
        URL url = new URL(urlString);
        connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        OutputStream output = connection.getOutputStream();
        dataOutoutStream = new DataOutputStream(output);

        // Send normal param.
        writeParameter(dataOutoutStream, boundary, "app_name", "My App Name");
        writeParameter(dataOutoutStream, boundary, "app_version", "2.04");

        // Send binary file.
        writeBinaryFile(output, dataOutoutStream, boundary, binaryFile);

        // End of multipart/form-data.
        dataOutoutStream.writeBytes("--" + boundary + "--" + CRLF);

         Log.v(tag, "response code " +  connection.getResponseCode());
    } catch(FileNotFoundException e) {
        e.printStackTrace();
    }  catch(UnsupportedEncodingException e) {
        e.printStackTrace();
    }  catch(IOException e) {
        e.printStackTrace();
    } finally {
        if (dataOutoutStream != null)
            try {
                dataOutoutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

Here is the code for my writeParameter method

private static void writeParameter(DataOutputStream dataOutputStream, String boundary, String paramName, String paramValue) throws IOException {
    dataOutputStream.writeBytes(TWO_HYPHENS + boundary + CRLF);
    dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + paramName + "\"" + CRLF);
    dataOutputStream.writeBytes("Content-Type: text/plain; charset=" + CHARSET_UTF_8 + CRLF);
    dataOutputStream.writeBytes(CRLF);
    dataOutputStream.writeBytes(paramValue + CRLF);
    dataOutputStream.flush();
}

Here is the writeBinaryFile method

private static void writeBinaryFile(OutputStream output, DataOutputStream dataOutputStream, String boundary, File binaryFile) throws IOException {
        dataOutputStream.writeBytes(TWO_HYPHENS + boundary + CRLF);
        dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"" + CRLF);
        dataOutputStream.writeBytes("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()) + CRLF);
        dataOutputStream.writeBytes("Content-Transfer-Encoding: binary" + CRLF);
        dataOutputStream.writeBytes(CRLF);
        dataOutputStream.flush();
        InputStream input = null;
        try {
            input = new FileInputStream(binaryFile);
            byte[] buffer = new byte[1024];
            for (int length = 0; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
            // Important! Output cannot be closed. Close of writer will close output as well.
            output.flush();
        } finally {
        if (input != null) 
            input.close();
        }
        dataOutputStream.writeBytes(CRLF);
        dataOutputStream.flush();
    }

May I know what I might be doing wrong.

I did check the Stackoverflow posts here and here.

Community
  • 1
  • 1
achie
  • 4,716
  • 7
  • 45
  • 59
  • What are you seeing when you run this and how do you know the connection is not being made? As you are using HTTPS your server may be needing client certs to connect. – rnk Jun 01 '13 at 00:34
  • I am seeing nothing. I have debug statements through out and in the exception blocks. Everything executes and it goes to the finally block and no exceptions are thrown. The server certificates are not the issue as I also tried it on our server which does not need certificates. We are checking the logs on server but I do not see the request there. – achie Jun 01 '13 at 00:50
  • I did not do the getResponseCode call earlier. Now it is sending the file and the parameters for file but not the other parameters from my writeParams method. I updated my post as such. – achie Jun 01 '13 at 01:53
  • Your problem is sending the file or the params ? the title and question body disagree !?! – Mr_and_Mrs_D Sep 17 '13 at 18:21
  • My problem was sending the file along with the parameters. Not one or the other. But I have since then solved the issue. – achie Sep 17 '13 at 22:25
  • sorry for reviving this 3 year old question, but how did you resolve it? I'm trying the same thing now. I'm able to upload my file, but my other parameters aren't being passed up. – Blair Holmes Jul 29 '16 at 18:48

0 Answers0