Android實(shí)現(xiàn)多參數(shù)文件和數(shù)據(jù)上傳
本文實(shí)例為大家分享了Android實(shí)現(xiàn)多參數(shù)文件和數(shù)據(jù)上傳的具體代碼,供大家參考,具體內(nèi)容如下
上代碼:
/**
* 可傳入多張圖片和參數(shù)
*
* @param actionUrl
* 發(fā)送地址
* @param params
* 文本參數(shù)
* @param files
* 文件參數(shù)
* @return
* @throws IOException
*/
public static String mulpost(String actionUrl, Map<String, String> params,
Map<String, File> files) throws IOException {
String result = "";
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);// 允許輸入
conn.setDoOutput(true);// 允許輸出
conn.setUseCaches(false);
conn.setRequestMethod("POST"); // Post方式
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);
// 首先組拼文本類型的參數(shù)
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\""
+ entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStream outStream = new DataOutputStream(
conn.getOutputStream());
outStream.write(sb.toString().getBytes());
// 發(fā)送文件數(shù)據(jù)
if (files != null)
for (Map.Entry<String, File> file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
+ file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
// 請(qǐng)求結(jié)束標(biāo)志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
result = br.readLine();
outStream.close();
conn.disconnect();
return result;
}
方法就是這樣的。
使用時(shí)可封裝在自己的類里直接調(diào)用即可,記得加網(wǎng)絡(luò)訪問(wèn)和讀取系統(tǒng)文件的權(quán)限哦。
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
由于上傳是耗時(shí)操作,必須得要弄在另一個(gè)線程中調(diào)用才可以。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android文件上傳示例分享(android圖片上傳)
- Android中發(fā)送Http請(qǐng)求(包括文件上傳、servlet接收)的實(shí)例代碼
- Android基于Http協(xié)議實(shí)現(xiàn)文件上傳功能的方法
- android選擇視頻文件上傳到后臺(tái)服務(wù)器
- Android使用xUtils3.0實(shí)現(xiàn)文件上傳
- 使用Android的OkHttp包實(shí)現(xiàn)基于HTTP協(xié)議的文件上傳下載
- Android關(guān)于FTP文件上傳和下載功能詳解
- Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
- 基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳
- Android引用開源框架通過(guò)AsyncHttpClient實(shí)現(xiàn)文件上傳
相關(guān)文章
Android中Service(后臺(tái)服務(wù))詳解
這篇文章主要介紹了Android中Service(后臺(tái)服務(wù))詳解,本文講解了Service的概念、作用、生命周期、啟動(dòng)方式和代碼實(shí)例等內(nèi)容,需要的朋友可以參考下2015-06-06
Android調(diào)用默認(rèn)瀏覽器打開指定Url的方法實(shí)例
業(yè)務(wù)員有需求要將一個(gè)wap站在手機(jī)上以App的形式打開,還不要嵌套WebView,只能以瀏覽器打開.查了點(diǎn)資料,就有了下面這代碼2013-09-09
Android自定義button點(diǎn)擊效果的兩種方式
這篇文章主要為大家詳細(xì)介紹了Android自定義button點(diǎn)擊效果的兩種方式,感興趣的小伙伴們可以參考一下2016-05-05
Android自定義簡(jiǎn)單的頂部標(biāo)題欄
這篇文章主要為大家詳細(xì)介紹了Android自定義簡(jiǎn)單的頂部標(biāo)題欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

