Android實現(xiàn)多參數(shù)文件和數(shù)據(jù)上傳
更新時間:2018年12月25日 14:11:23 作者:靜下心來靜下心來
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)多參數(shù)文件和數(shù)據(jù)上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(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()); } // 請求結(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; }
方法就是這樣的。
使用時可封裝在自己的類里直接調(diào)用即可,記得加網(wǎng)絡(luò)訪問和讀取系統(tǒng)文件的權(quán)限哦。
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
由于上傳是耗時操作,必須得要弄在另一個線程中調(diào)用才可以。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- android文件上傳示例分享(android圖片上傳)
- Android中發(fā)送Http請求(包括文件上傳、servlet接收)的實例代碼
- Android基于Http協(xié)議實現(xiàn)文件上傳功能的方法
- android選擇視頻文件上傳到后臺服務(wù)器
- Android使用xUtils3.0實現(xiàn)文件上傳
- 使用Android的OkHttp包實現(xiàn)基于HTTP協(xié)議的文件上傳下載
- Android關(guān)于FTP文件上傳和下載功能詳解
- Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
- 基于標(biāo)準(zhǔn)http實現(xiàn)Android多文件上傳
- Android引用開源框架通過AsyncHttpClient實現(xiàn)文件上傳
相關(guān)文章
Android調(diào)用默認(rèn)瀏覽器打開指定Url的方法實例
業(yè)務(wù)員有需求要將一個wap站在手機上以App的形式打開,還不要嵌套WebView,只能以瀏覽器打開.查了點資料,就有了下面這代碼2013-09-09