您的位置:首页 > 移动开发 > Android开发

android客户端多文件上传(最简单和强悍版本),一看就懂

2016-03-15 09:44 633 查看
一、本文本主要解决客户端中一次性上传多个文件的代码整理

1、直接上代码

private void fileuploadMore1(ArrayList<String> imgName, String uid,

String title, String context) throws URISyntaxException,

UnsupportedEncodingException {

// StringEntity reqEntity = new StringEntity(context, HTTP.UTF_8);

String actionUrl = HttpUrl.actionUrl + "?UID=" + uid + "&SUBJECT="

+ URLEncoder.encode(title, "UTF-8") + "&CONTENT="

+ URLEncoder.encode(context, "UTF-8");

// actionUrl= URLEncoder.encode(actionUrl, "UTF-8");

URI url = new URI(actionUrl);

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(url);

MultipartEntity multiEntity = new MultipartEntity(

HttpMultipartMode.BROWSER_COMPATIBLE, null, null);

for (String file : imgName) {

FileBody body = new FileBody(new File(file));// (FileUrl +file));

multiEntity.addPart(file, body);

}

post.setEntity(multiEntity);

try {

System.out.println("data-0");

HttpResponse response = client.execute(post);

int statusCode = response.getStatusLine().getStatusCode();

System.out.println("data-1");

String result = EntityUtils.toString(response.getEntity(), "utf-8");

JSONObject jsObject = null;

try {

jsObject = new JSONObject(result);

} catch (JSONException e1) {

e1.printStackTrace();

}

if (statusCode == 200) {

System.out.println("data-1" + "success");

handler.sendEmptyMessage(0x01);

} else {

System.out.println("data-1" + "fail else");

handler.sendEmptyMessage(0x02);

}

} catch (ClientProtocolException e) {

e.printStackTrace();

System.out.println("data-1" + "fail e1");

handler.sendEmptyMessage(0x02);

} catch (IOException e) {

e.printStackTrace();

System.out.println("data-1" + "fail e2");

handler.sendEmptyMessage(0x02);

}

}

说明 actionUrl 写你自己服务器端支持图片多文件上传接口,这个需要服务器端一起配合,我这只提供客户端代码

2 需要增加架包httpmime 架包。

3 使用非常简单,而且上传功能实现非常好
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: