Android用戶端多檔案上傳

來源:互聯網
上載者:User

標籤:

在web開發中,多檔案上傳時是非常方便的,直接使用Http協議提交資料即可。格式如下:

   <FORM METHOD=POST ACTION="http://192.168.1.101:8083/upload/servlet/UploadServlet" enctype="multipart/form-data">            <INPUT TYPE="text" NAME="name">            <INPUT TYPE="text" NAME="id">            <input type="file" name="imagefile"/>            <input type="file" name="zip"/>         </FORM>

但是在Android用戶端中,如果文本和檔案一起上傳時就比較麻煩,需要對Http協議很熟悉才可以做到的。

一、HTTP協議HTTP協議是一個很很複雜的過程,我們在做web開發時,底層其實都已經封裝好了,可以使用即可,我們所需要做的就是對HTTP請求格式很熟悉。HTTP協議分為要求方法URI協議/版本,要求標頭,請求體。
<span style="white-space:pre"></span>GET/sample.jspHTTP/1.1                    //要求方法/請求路徑/協議版本<span style="white-space:pre"></span>Accept:image/gif.image/jpeg,*/*          <span style="white-space:pre"></span>Accept-Language:zh-cn<span style="white-space:pre"></span>Connection:Keep-Alive<span style="white-space:pre"></span>Host:localhost<span style="white-space:pre"></span>User-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)<span style="white-space:pre"></span>Accept-Encoding:gzip,deflate<span style="white-space:pre"></span>username=jinqiao&password=1234
這是一個完整的HTTP請求。請求的協議頭主要包括以下一些內容:要求標頭和請求體中間有一個空格,這個空格表示要求標頭已經結束,下面的是請求體。回應標頭:這即為整個的HTTP請求。
二、Android中的多檔案上傳Android中使用HTTP GET或者POST方法提交資料在前文已經介紹過了。
代碼在struts上傳檔案一文中已經貼出來了。
在代碼中 ,我們需要自己動手完整的構造出整個HTTP請求的格式。但有一個不同的地方;首先是Content-Type:multipart/form-data;boundary=---------------------------7da2137580612;因為表單類型為:multipart/form-data,上傳檔案使用的不在是HTTP協議,不在是name="--"的方式了,而是使用分隔字元欄位內容分隔字元欄位內容比如:要求標頭---------------------------7da2137580612
Content-Disposition:form-data;name=“username”

zhuzg
---------------------------7da2137580612Content-Disposition:form-data;name=“image”;filename=“123.jpg”Content-Type:application/octet-stream
---------------------------7da2137580612----------- //結束標誌

Android用戶端多檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.