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

Source: Internet
Author: User
Tags gettext

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

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.