Upload Photo using HttpPost MultiPartEntityBuilder

enter image description here Check that jar in Order and Export tab and run.


There is my working solution for sending image with post, using apache http libraries (very important here is boundary add It won't work without it in my connection):

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(StaticData.AMBAJE_SERVER_URL + StaticData.AMBAJE_ADD_AMBAJ_TO_GROUP);

String boundary = "-------------" + System.currentTimeMillis();

httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);

ByteArrayBody bab = new ByteArrayBody(imageBytes, "pic.png");
StringBody sbOwner = new StringBody(StaticData.loggedUserId, ContentType.TEXT_PLAIN);
StringBody sbGroup = new StringBody("group", ContentType.TEXT_PLAIN);

HttpEntity entity = MultipartEntityBuilder.create()
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .setBoundary(boundary)
                    .addPart("group", sbGroup)
                    .addPart("owner", sbOwner)
                    .addPart("image", bab)
                    .build();

httpPost.setEntity(entity);

try {
       HttpResponse response = httpclient.execute(httpPost);
       ...then reading response

Better you pass the path of image file. Below is my code which I used to upload image to server.

public class UploadProductDetails {


    public void uploadProductDetails(String filePath, String fileName)
    {

        InputStream inputStream;
        try
        {
            inputStream = new FileInputStream(new File(filePath));
            byte[] data;
            try
            {
                data = IOUtils.toByteArray(inputStream);

                HttpClient httpClient = new DefaultHttpClient();

                httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                        System.getProperty("http.agent"));



                HttpPost httpPost = new HttpPost("http://ipaddress");


                InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "abc.png");
                MultipartEntity multipartEntity = new MultipartEntity();
                multipartEntity.addPart("file", inputStreamBody);


                httpPost.setEntity(multipartEntity);
                HttpResponse httpResponse = httpClient.execute(httpPost);

                // Handle response back from script.
                if(httpResponse != null) {
                    //Toast.makeText(getBaseContext(),  "Upload Completed. ", 2000).show();

                } else { // Error, no response.
                    //Toast.makeText(getBaseContext(),  "Server Error. ", 2000).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }

}