Sending images can be done using the Httpcomponents libraries. Download the latest HttpClient (currently4.0.1) binary with dependencies package and copy and to apache-mime4j-0.6.jar httpmime-4.0.1.jar y Our project and add them to your Java build path.
You'll need to add the following imports to your class.
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;
Now the can create a to MultipartEntity attach a image to your POST request. The following code shows an example of what do this:
public void post (String URL, List namevaluepairs) {
Httpclienthttpclient = new Defaulthttpclient ();
Httpcontextlocalcontext = new Basichttpcontext ();
Httpposthttppost = new HttpPost (URL);
try{
multipartentity entity = new multipartentity(httpmultipartmode.browser_compatible);
for (int index=0; index < namevaluepairs.size (); index++) {
if (Namevaluepairs.get (index). GetName (). Equalsignorecase ("image")) {
If the key equals to ' image ', we use filebody to transfer thedata
Entity.addpart (Namevaluepairs.get (index). GetName (), Newfilebody (New File (Namevaluepairs.get (index). GetValue ()))) ;
} else {
Normal String Data
Entity.addpart (Namevaluepairs.get (index). GetName (), Newstringbody (Namevaluepairs.get (index). GetValue ()));
}
}
Httppost.setentity (entity);
HttpResponse response = Httpclient.execute (Httppost,localcontext);
} catch (IOException e) {
E.printstacktrace ();
}
}
This method visually written well, first reserved for use