標籤:throw msi 標籤 pen 方法 tip uid 數值 size
RFC1867協議介紹
RFC1867協議主要是在HTTP協議的基礎上為INPUT標籤添加了file屬性。同一時候限定了Form的method必須為POST,ENCTYPE必須為multipart/form-data。 其他屬性標籤, <INPUT TYPE=file>標記能夠有一個VALUE屬性來指定預設的檔案名 ,能夠用“SIZE=寬,高”來指定SIZE屬性 。
multipart/form-data
multipart/form-data的媒體內容遵從RFC 1521所規定的多部分的資料流規則。
它主要被用來描寫敘述表單填寫後返回的資料。在一個表單中(這裡指的是HTML,當然其它一些應用也可 能使用表單),有一數列欄位提供給使用者進行填寫。每一個欄位都有自己的名字。
在一個確定 的表單中。每一個名字都是唯一的。
multipart/form-data由多個部分組成,每一部分都有一個content-disposition標題頭,它的 值是"form-data"。它的屬性指明了其在表單內的欄位名。舉例來說,‘content-disposition: form-data; name="xxxxx"‘,這裡的xxxxx就是相應於該欄位的欄位名。假設欄位名包括非 ASCII碼字元的話。還應該依照RFC 1522裡面所規定的方法進行編碼。
對全部的多部分MIME類型來說,每一部分有一個可選的Content-Type,預設的值是 text/plain。假設檔案的內容是通過表單填寫上傳返回的話。那麼輸入的檔案就被定義為 application/octet-stream,或者,假設知道是什麼類型的話,就定義為對應的媒體類型。如 果一個表單返回多個檔案,那麼它們就作為multipart/form-data中所結合的multipart/mixed 被返回。
假設所傳送的內容不符合預設的編碼方式的話。該部分都將被編碼,並加上 "content-transfer-encoding"的標題頭。
Android Post上傳檔案的實現
Android POST方式上傳檔案,能夠基於通過 RFC1867協議來實現。
/** * * @param urlPath * @param params * map 參數 <參數名稱 , 參數值> * @param fileParams * map 檔案類型 參數 <參數名稱 , 檔案路徑> * */public String postFile(String urlPath, Map<String, Object> params,Map<String, String> fileParams) throws FileNotFoundException {String PREFIX = "--"; // 首碼String LINE_END = "\r\n"; // 換行String BOUNDARY = UUID.randomUUID().toString(); // 邊界標識URL url;HttpURLConnection connection;try {url = new URL(urlPath);connection = (HttpURLConnection) url.openConnection();// 設定逾時時間connection.setReadTimeout(readTimeOut);connection.setConnectTimeout(connectTimeOut);// 請求方式connection.setRequestMethod("POST");connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");// 開啟輸入資料流connection.setDoInput(true);// 開啟輸出資料流connection.setDoOutput(true);// 關閉緩衝connection.setUseCaches(false);// 設定編碼connection.setRequestProperty("Charset", "utf-8");connection.setRequestProperty("connection", "keep-alive");connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");// 設定內容類型及定義BOUNDARYconnection.setRequestProperty("Content-Type", "multipart/form-data"+ ";boundary=" + BOUNDARY);// 擷取輸出資料流DataOutputStream dos = new DataOutputStream(connection.getOutputStream());StringBuffer sb = null;String result = "";String paramStr;// 發送非檔案參數if (mParams != null && mParams.size() > 0) {Iterator<String> it = mParams.keySet().iterator();while (it.hasNext()) {sb = null;sb = new StringBuffer();String key = it.next();Object value = mParams.get(key);sb.append(PREFIX).append(BOUNDARY).append(LINE_END);sb.append("Content-Disposition: form-data; name=\"").append(key).append("\"").append(LINE_END).append(LINE_END);sb.append(value).append(LINE_END);paramStr = sb.toString();dos.write(paramStr.getBytes());dos.flush();}}paramStr = null;// 傳送檔案參數,讀取檔案流寫入post輸出資料流if (mFileParams != null && !mFileParams.isEmpty()) {Iterator<Entry<String, String>> fileIter = mFileParams.entrySet().iterator();while (fileIter.hasNext()) {sb = null;sb = new StringBuffer();Entry<String, String> entry = fileIter.next();String fileKey = entry.getKey();String filePath = entry.getValue();File file = new File(filePath);if (file.exists() == false) {throw new FileNotFoundException();}// 設定邊界標示,設定 Content-Disposition頭傳入檔案流sb.append(PREFIX).append(BOUNDARY).append(LINE_END);sb.append("Content-Disposition:form-data; name=\""+ fileKey + "\"; filename=\"" + file.getName()+ "\"" + LINE_END);sb.append("Content-Type:" + CONTENT_TYPE + LINE_END);sb.append(LINE_END);dos.write(sb.toString().getBytes());InputStream is = new FileInputStream(file);byte[] bytes = new byte[1024];int len = 0;while ((len = is.read(bytes)) != -1) {dos.write(bytes, 0, len);}is.close();dos.write(LINE_END.getBytes());dos.flush();}byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes();dos.write(end_data);dos.flush();}dos.close();int res = getResponseCode();// 返回成功if (res == 200) {InputStream input = conn.getInputStream();StringBuffer sb1 = new StringBuffer();int ss;while ((ss = input.read()) != -1) {sb1.append((char) ss);}result = sb1.toString();return result;} else {}} catch (MalformedURLException e) {Log.i(TAG, "MalformedURLException error");} catch (IOException e) {Log.i(TAG, "IOException error");}return null;}
</pre><pre>
文章地址 :http://blog.csdn.net/jmq_0000/article/details/30244297
Android Http POST檔案上傳之-----RFC1867協議