Download HTTPCLIENT under HttpComponents under apache
---------- Address is http://hc.apache.org/downloads.cgi
Mainly used in the httpmime-4.1.2.jar package
Android client:
The following is the jsp form requesting action (for test)
(
<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 = "" listKey = "Itemid" listValue = "itemname"> </s: select> <br/>
<S: textfield label = "dish price" name = "price"> </s: textfield> <br/>
<S: file label = "dish image" name = "foodimg"> </s: file> <br/>
<S: textarea label = "dish label" name = "foodtab" cols = "20" cssStyle = ""> </s: textarea> <br/>
<S: textfield label = "dish status" name = "state"> </s: textfield> <br/>
<S: submit value = "add"/>
</Div>
</Form>
Simulate the construction of 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 image form data
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.exe cute (httppost );
If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK)
{
MakeToase ("uploaded successfully", true );
If (this. image. exists ())
This. image. delete ();
}
Else
{
MakeToase ("Upload Failed", true );
}
)
Server: action Configuration
<Action name = "AddFoodStyle" class = "com. ordermeal. xiao. action. AddFoodStyle">
<Result name = "success" type = "redirect">/ShowAddFoodStyle </result>
</Action>
Write Action
Public class addfoodstyle extends actionsupport {
/**
*
*/
Private Static final long serialversionuid =-8380963167787044860l;
Private string foodname;
Private integer foodstyle;
Private double price;
// Receives the uploaded file
Private file foodimg;
Private string foodimgfilename;
Private string foodimgcontenttype;
Private string foodtab;
Private integer state;
, And omit the 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 the uploaded file
FileUtil. copyFile (foodimg, path + "/images/" + foodimgFileName );
Return SUCCESS;
}