日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 上传字符串,Android中发送Http请求(包括文件上传、servlet接收)的实例代码...

發布時間:2023/11/27 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 上传字符串,Android中发送Http请求(包括文件上传、servlet接收)的实例代码... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**

* 通過拼接的方式構造請求內容,實現參數傳輸以及文件傳輸

* @param actionUrl

* @param params

* @param files

* @return

* @throws IOException

*/

public static String post(String actionUrl, Map params,

Map files) throws IOException {

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");

conn.setRequestProperty("connection", "keep-alive");

conn.setRequestProperty("Charsert", "UTF-8");

conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

// 首先組拼文本類型的參數

StringBuilder sb = new StringBuilder();

for (Map.Entry 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());

// 發送文件數據

if(files!=null)

for (Map.Entry 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());

}

//請求結束標志

byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();

outStream.write(end_data);

outStream.flush();

// 得到響應碼

int res = conn.getResponseCode();

if (res == 200) {

InputStream in = conn.getInputStream();

int ch;

StringBuilder sb2 = new StringBuilder();

while ((ch = in.read()) != -1) {

sb2.append((char) ch);

}

}

outStream.close();

conn.disconnect();

return in.toString();

}

總結

以上是生活随笔為你收集整理的android 上传字符串,Android中发送Http请求(包括文件上传、servlet接收)的实例代码...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:android 上传字符串,Android中发送Http请求