Android-httpclient uploads information (including images) to the server

Source: Internet
Author: User

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.