Need to download httpclient under the Httpcomponents project under the Apache Company
----------Address is http://hc.apache.org/downloads.cgi
The main use of the Httpmime-4.1.2.jar bag
Android Client:
The following is the JSP form that requested the action (for testing)
(
<form action= "Addfoodstyle" enctype= "Multipart/form-data" method= "POST" >
<div style= "width:300px;" >
<s:textfield label= "dish name" name= "Foodname" ></s:textfield><br/>
<s:select name= "Foodstyle" list= "list" label= "Food category" listkey= "Itemid" listvalue= "ItemName" > </s:select> <br/>
<s:textfield label= "Menu" name= "Price" ></s:textfield><br/>
<s:file label= "Food picture" name= "foodimg" ></s:file><br/>
<s:textarea label= "food label" Name= "Foodtab" cols= "cssstyle=" "></s:textarea><br/>
<s:textfield label= "Food status" name= "state" ></s:textfield><br/>
<s:submit value= "Add"/>
</div>
</form>
Simulation constructs the above request form:
Private String url= "Http://192.168.2.189:8080/MyOrderMeal/AddFoodStyle";
HttpClient httpclient= new Defaulthttpclient ();
HttpPost httppost= new HttpPost (URL);
Multipartentity mulentity = new multipartentity (httpmultipartmode.browser_compatible);
Mulentity.addpart ("Foodname", New Stringbody (Foodname.gettext (). toString (). Trim ()));
Mulentity.addpart ("Foodstyle", New Stringbody (Foodstyle.gettext (). toString (). Trim ()));
Mulentity.addpart ("Price", New Stringbody (Foodprice.gettext (). toString (). Trim ()));
Add Picture form data
Filebody filebody = new Filebody (this.image);
Mulentity.addpart ("Foodimg", filebody);
Mulentity.addpart ("Foodtab", New Stringbody (Foodtab.gettext (). toString (). Trim ()));
Mulentity.addpart ("state", New Stringbody ("1"));
Httppost.setentity (mulentity);
HttpResponse response = Httpclient.execute (HttpPost);
if (Response.getstatusline (). Getstatuscode () ==HTTPSTATUS.SC_OK)
{
Maketoase ("Upload succeeded", true);
if (This.image.exists ())
This.image.delete ();
}
Else
{
Maketoase ("Upload failed", true);
}
)
Service side: Configuration of action
<action name= "Addfoodstyle" class= "Com.ordermeal.xiao.action.AddFoodStyle";
<result name= "Success" type= "redirect" >/showaddfoodstyle</result
</action>
Action Writing
public class Addfoodstyle extends actionsupport{
/**
*
*/
Private static final long serialversionuid = -8380963167787044860l;
Private String Foodname;
Private Integer Foodstyle;
Private Double Price;
Receive upload files
Private File foodimg;
Private String Foodimgfilename;
Private String Foodimgcontenttype;
Private String Foodtab;
Private Integer State;
、、、、 omit Get Set method
@Override
Public String Execute () throws Exception {
Foodstyledao FSD = Daofactory.getfoodstyledao ();
Foodstyle foodstyleob= new Foodstyle ();
Foodstyleob.setfoodname (Foodname);
Foodstyleob.setmystyletype (Foodstyle);
Foodstyleob.setfoodprice (price);
Foodstyleob.setimageurl (Foodimgfilename);
Foodstyleob.setfoodtab (Foodtab);
Foodstyleob.setfdstystate (state);
Fsd.addfoodstyle (FOODSTYLEOB);
String path= Servletactioncontext.getservletcontext (). Getrealpath ("/");
Save upload File
Fileutil.copyfile (foodimg, path+ "/images/" +foodimgfilename);
return SUCCESS;
}
//reference http://blog.csdn.net/liwei3gjob/article/details/7870353
Android-httpclient uploading information (including images) to the server