I am trying to post media files to a server using HttpClient. My code works fine for image files, however the video files (mp4) can't be replayed. My code for posting the files:
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(REMOTE + "/add_file.php");
MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();
ContentBody cbFile = null;
String mimeType = "";
if (file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg")) {
mimeType = "image/jpeg";
} else if (file.getName().endsWith(".mp4")) {
mimeType = "video/mp4";
}
mpEntity.addTextBody("recipient_phone", recipientPhoneStr);
mpEntity.addTextBody("sender_phone", "55000");
mpEntity.addTextBody("sender_key", "my_secret");
mpEntity.addTextBody("file_name", file.getName());
mpEntity.addTextBody("userfile", encodeFileToBase64Binary(file));
httppost.setEntity(mpEntity.build());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (response.getStatusLine().toString().compareTo(HTTP_ERROR) == 0) {
throw new IOException(HTTP_ERROR);
}
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
The files are Base64 encoded using Base64.encodeBase64String(bytes);