Android Http POST文件上传之-----RFC1867协议
 RFC1867協(xié)議介紹
 ? ? ? ? ? ?RFC1867協(xié)議主要是在HTTP協(xié)議的基礎(chǔ)上為INPUT標(biāo)簽增加了file屬性,同時(shí)限定了Form的method必須為POST,ENCTYPE必須為multipart/form-data。?其它屬性標(biāo)簽,?<INPUT TYPE=file>標(biāo)記可以有一個(gè)VALUE屬性來指定默認(rèn)的文件名?,可以用“SIZE=寬,高”來指定SIZE屬性?。
?
 multipart/form-data
 
 ? ? ? ?multipart/form-data的媒體內(nèi)容遵從RFC 1521所規(guī)定的多部分的數(shù)據(jù)流規(guī)則。它主要被用來描述表單填寫后返回的數(shù)據(jù)。在一個(gè)表單中(這里指的是HTML,當(dāng)然其他一些應(yīng)用也可?能使用表單),有一系列字段提供給用戶進(jìn)行填寫,每個(gè)字段都有自己的名字。在一個(gè)確定 的表單中,每個(gè)名字都是唯一的。 ?
 ? ? ? ?multipart/form-data由多個(gè)部分組成,每一部分都有一個(gè)content-disposition標(biāo)題頭,它的 值是"form-data",它的屬性指明了其在表單內(nèi)的字段名。舉例來說,'content-disposition: ?form-data; name="xxxxx"',這里的xxxxx就是對應(yīng)于該字段的字段名。如果字段名包含非 ASCII碼字符的話,還應(yīng)該按照RFC 1522里面所規(guī)定的方法進(jìn)行編碼。 ?
 ? ? ? ? 對所有的多部分MIME類型來說,每一部分有一個(gè)可選的Content-Type,默認(rèn)的值是 text/plain。如果文件的內(nèi)容是通過表單填寫上傳返回的話,那么輸入的文件就被定義為 application/octet-stream,或者,如果知道是什么類型的話,就定義為相應(yīng)的媒體類型。如 果一個(gè)表單返回多個(gè)文件,那么它們就作為multipart/form-data中所結(jié)合的multipart/mixed 被返回。 ?
 如果所傳送的內(nèi)容不符合默認(rèn)的編碼方式的話,該部分都將被編碼,并加上 "content-transfer-encoding"的標(biāo)題頭。?
 
 
 Android?Post上傳文件的實(shí)現(xiàn) 
? ? ? ? ? ?Android POST方式上傳文件,可以基于通過?RFC1867協(xié)議來實(shí)現(xiàn)。
 
 
/*** * @param urlPath* @param params* map 參數(shù) <參數(shù)名稱 , 參數(shù)值>* @param fileParams* map 文件類型 參數(shù) <參數(shù)名稱 , 文件路徑>* */public String postFile(String urlPath, Mapparams,MapfileParams) throws FileNotFoundException {String PREFIX = "--"; // 前綴String LINE_END = "\r\n"; // 換行String BOUNDARY = UUID.randomUUID().toString(); // 邊界標(biāo)識URL url;HttpURLConnection connection;try {url = new URL(urlPath);connection = (HttpURLConnection) url.openConnection();// 設(shè)置超時(shí)時(shí)間connection.setReadTimeout(readTimeOut);connection.setConnectTimeout(connectTimeOut);// 請求方式connection.setRequestMethod("POST");connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");// 開啟輸入流connection.setDoInput(true);// 開啟輸出流connection.setDoOutput(true);// 關(guān)閉緩存connection.setUseCaches(false);// 設(shè)置編碼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)");// 設(shè)置內(nèi)容類型及定義BOUNDARYconnection.setRequestProperty("Content-Type", "multipart/form-data"+ ";boundary=" + BOUNDARY);// 獲取輸出流DataOutputStream dos = new DataOutputStream(connection.getOutputStream());StringBuffer sb = null;String result = "";String paramStr;// 發(fā)送非文件參數(shù)if (mParams != null && mParams.size() > 0) {Iteratorit = 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; // 發(fā)送文件參數(shù),讀取文件流寫入post輸出流 if (mFileParams != null && !mFileParams.isEmpty()) { Iterator> fileIter = mFileParams .entrySet().iterator(); while (fileIter.hasNext()) { sb = null; sb = new StringBuffer(); Entryentry = fileIter.next(); String fileKey = entry.getKey(); String filePath = entry.getValue(); File file = new File(filePath); if (file.exists() == false) { throw new FileNotFoundException(); } // 設(shè)置邊界標(biāo)示,設(shè)置 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; }
 
 
總結(jié)
以上是生活随笔為你收集整理的Android Http POST文件上传之-----RFC1867协议的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【HTTP】另类的POST头数据 RFC
- 下一篇: Multipart/form-data
