android-HttpClient上傳資訊(包括圖片)到服務端

來源:互聯網
上載者:User

需要下載apache公司下的HttpComponents項目下的HTTPCLIENT

----------地址為http://hc.apache.org/downloads.cgi

主要是用到了httpmime-4.1.2.jar包

android用戶端:

以下是請求action的jsp表單(測試用)

<form action="AddFoodStyle" enctype="multipart/form-data" method="post">   
    <div style="width:300px;">

        <s:textfield label="菜式名稱" name="foodname"></s:textfield><br/>       

        <s:select name="foodstyle" list="list" label="菜式類別" listKey="Itemid" listValue="itemname"  > </s:select><br/>       

        <s:textfield label="菜式價格" name="price"></s:textfield><br/>       
        
        <s:file label="菜式圖片" name="foodimg"></s:file><br/>       
        
        <s:textarea label="菜式標籤" name="foodtab" cols="20"  cssStyle=""></s:textarea><br/>       
        
        <s:textfield label="菜式狀態" name="state"></s:textfield><br/>       
        
        <s:submit value="添加"/>
        </div>       
    </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()));  

       //添加圖片表單資料       
        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("上傳成功",true);
            if(this.image.exists())
            this.image.delete();
        }
        else
        {
            makeToase("上傳失敗",true);
        }

服務端:action的配置

     <action name="AddFoodStyle" class="com.ordermeal.xiao.action.AddFoodStyle">
          <result name="success" type="redirect">/ShowAddFoodStyle</result>
     </action>
  
    action的編寫

public class AddFoodStyle extends ActionSupport{
    /**
     *
     */
    private static final long serialVersionUID = -8380963167787044860L;
    
    private String foodname;
    private Integer foodstyle;
    private Double price;

   

   //接收上傳檔案

    private File foodimg;
    private String foodimgFileName;
    private String foodimgContentType;
    
    private String foodtab;

    private Integer state;

、、、、省略get  set方法

@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("/");

       //儲存上傳檔案

       FileUtil.copyFile(foodimg, path+"/images/"+foodimgFileName);

       return SUCCESS;

    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.