1

I have a multipart/form-data request code, but it gives an error. I checked for errors, but I could not find anything. Could you help me to solve this problem? Unfortunately, I myself can not cope.

        URL serverUrl = new URL("https...");
        HttpURLConnection urlConnection = (HttpURLConnection) serverUrl.openConnection();

        String boundaryString = generateBoundary();
        // Indicate that we want to write to the HTTP request body
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);

        // Indicate that we want to write some data as the HTTP request body
        urlConnection.setDoOutput(true);

        OutputStream outputStreamToRequestBody = urlConnection.getOutputStream();
        BufferedWriter httpRequestBodyWriter =
            new BufferedWriter(new OutputStreamWriter(outputStreamToRequestBody));

        // token
        httpRequestBodyWriter.write("\n\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"token\"\n\n");
        httpRequestBodyWriter.write(mMyApp.token);

        //orderId
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"orderId\"\n\n");
        httpRequestBodyWriter.write(order_id);

        //docType
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"docType\"\n\n");
        httpRequestBodyWriter.write(photo.i_doc_type);

        //leftEyeX
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"leftEyeX\"\n\n");
        httpRequestBodyWriter.write(photo.i_left_eye_x);

        //leftEyeY
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"leftEyeY\"\n\n");
        httpRequestBodyWriter.write(photo.i_left_eye_y);

        //rightEyeX
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"rightEyeX\"\n\n");
        httpRequestBodyWriter.write(photo.i_right_eye_x);

        //rightEyeY
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"rightEyeY\"\n\n");
        httpRequestBodyWriter.write(photo.i_right_eye_y);

        // Include image
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data;"
                + "name=\"" + photo.photo_name + "\";"
                + "filename=\""+ photo.photo_name +".jpg\""
                + "\nContent-Type: image/jpg\n\n");
        httpRequestBodyWriter.write(photo.photo_data);

        // Mark the end of the multipart http request
        httpRequestBodyWriter.write("\n--" + boundaryString + "--\n");
        outputStreamToRequestBody.flush();
        httpRequestBodyWriter.flush();

        // Close the streams
        outputStreamToRequestBody.close();
        httpRequestBodyWriter.close();


        // Read response from web server, which will trigger the multipart HTTP request to be sent.
        BufferedReader httpResponseReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String lineRead;
        while((lineRead = httpResponseReader.readLine()) != null) {
            System.out.println(lineRead);
        }
        result = lineRead;   

Server response: "Unexpected end of stream. Is there an end boundary?". What am I doing wrong? What's my mistake?

Magic Max
  • 21
  • 3

0 Answers0