我有一个multipart / form-data请求代码,但它会出错。我检查了错误,但我找不到任何东西。你能帮我解决这个问题吗?不幸的是,我自己无法应付。
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;
服务器响应:"意外的流结束。是否有结束边界?"。 我究竟做错了什么?我的错是什么?