I am trying to upload an image to a server along with some JSON data that is collected from a form.
The server has authentication.
METHOD: post
HEADERS:
Authorization Basic d2Vic2VydmljZTpyM05hdTE3Rw==
Content-Type multipart/form-data;boundary=xxxxxxxx
BODY:
--xxxxxxxx
Content-Disposition: form-data; name="jsonFile"
Content-Type: application/json
{"model":"Premium","deviceLongitude":4.79337638,"pseudo":"nickname","deviceLatitude":45.7671507,"year":"2005","email":"[email protected]","deviceLocale":"fr_FR","title":"my picture"}
--xxxxxxxx
Content-Disposition: form-data; name="imgName"
Content-Type: image/jpeg
//Image data array
/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAAB
AAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAB4KAD
AAQAAAABAAACgAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQH/wAARCAKAAeADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAA
AAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF
--xxxxxxxx
public interface ApiConfig { static String BASE_URL=\"https://your base url/\"; @Multipart @POST(\"/images/upload\") Call<ServerResponse> uploadImage(@Part MultipartBody. Part image); or @POST Call<Object> uploadImage(@Url String url, @Part MultipartBody.
Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).
June 15, 2022 A multipart request is an HTTP request that HTTP clients create to send files and data to an HTTP server. A multipart message is made up of several parts. One part consists of a header and a body. The body can be any type of media and can contain text or binary data.
I know this post is a couple of years old, but I'd like to share my solution using MultiPartEntity from here available as part of the HttpClient download. I used version 4.2.5.
I originally used a process similar to above, which worked well until I started getting memory errors when I started supporting uploading of videos. I researched a lot of posts here and got a lot of good ideas. I used the Java Decompiler available here to look at the code in the Jar file to figure out how to put things together.
//filepath is passed in like /mnt/sdcard/DCIM/100MEDIA/VIDEO0223.mp4
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
String json = "{your_json_goes_here}";
File media = new File(filePath);
URI uri = your_uri_goes_here;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
StringBody jsonBody = new StringBody(json, "application/json", null);
FormBodyPart jsonBodyPart = new FormBodyPart("json", jsonBody);
String mimeType;
if (requestCode == Constants.ACTION_TAKE_VIDEO) {
mimeType = "video/" + extension;
} else {
// default to picture
mimeType = "image/" + extension;
}
FileBody fileBody = new FileBody(media, mimeType, "ISO-8859-1");
FormBodyPart fileBodyPart = new FormBodyPart(fileName, fileBody);
MultipartEntity mpEntity = new MultipartEntity(null, "xxBOUNDARYxx", null);
mpEntity.addPart(jsonBodyPart);
mpEntity.addPart(fileBodyPart);
post.addHeader("Content-Type", "multipart/mixed;boundary=xxBOUNDARYxx");
post.setEntity(mpEntity);
HttpResponse response = httpClient.execute(post);
InputStream data = response.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(data));
String responseLine;
StringBuilder responseBuilder = new StringBuilder();
while ((responseLine = bufferedReader.readLine()) != null) {
responseBuilder.append(responseLine);
}
System.out.println("Response: " + responseBuilder.toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With