Below is the code i use for simulating an image attachment using apache http client.
package com.edw.postsimulator;
import java.io.File;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class PostTest {
private static String url = "http://localhost:8083/RealsAppApi/images/add";
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpContext localContext = new BasicHttpContext();
Charset chars = Charset.forName("UTF-8");
// http content
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("username", new StringBody("edwin", chars));
entity.addPart("password", new StringBody("edwinpassword", chars));
// attach image
File fileToUpload = new File("F:\\no.jpg");
FileBody fileBody = new FileBody(fileToUpload, "application/octet-stream");
entity.addPart("image", fileBody);
httpPost.setEntity(entity);
// fire and log the responses
HttpResponse response = httpclient.execute(httpPost, localContext);
HttpEntity resEntity = response.getEntity();
System.out.println(EntityUtils.toString(resEntity));
}
}
These are the libraries i used for this project,

Hope it would help others, have fun (Y)