最近手头上需要批量上传一些保存到SD卡图片由于简单,过于忘记,写在博客中吧!同时也希望能帮到大家!

一 、 以下是一个Service类

package cn.com.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

import android.util.Log;

/**
* 批量上传服务类
* @author ChonZY
*
*/
public class DataService {
/**
* 批量上传图片
* @param path 服务器地址
* @param name 用户名
* @param filePath sd卡图片路径
* @return
* @throws Exception
*/
public static String sendDataByHttpClientPost(String path ,String name,List<File> filePath)throws Exception{

List<Part> partList = new ArrayList<Part>();
partList.add(new StringPart("user", name));
partList.add(new StringPart("picnumber", "4"));

for (int i = 0; i < filePath.size(); i++) {
String picPath = filePath.get(i).getPath();
Log.i("Other", "服务类 图片路径Service _ PicPath ="+picPath);
partList.add(new FilePart("file", new File(filePath.get(i).getPath())));
}

// 实例化上传数据的数组
//Part [] parts = {new StringPart("user", name),new FilePart("file", new File(filePath))};

Part[] parts = partList.toArray(new Part[0]);

Log.i("Other","new Part[0] _size:"+ parts.length);

PostMethod filePost = new PostMethod(path);

filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

int status = client.executeMethod(filePost);
Log.i("Other", "返回结果:"+status);
if(status == 200){

System.out.println( filePost.getResponseCharSet());
String result = new String(filePost.getResponseBodyAsString());


System.out.println("--"+result);
return result;
}else{
throw new IllegalStateException("服务器状态异常");
}


}
/**
* 单次上传图片or文件
* @param path
* @param name
* @param filePath
* @return
* @throws Exception
*/
public static String sendDataByHttpClientPost(String path ,String name,String filePath)throws Exception{

/*List<Part> partList = new ArrayList<Part>();
partList.add(new StringPart("user", name));

for (int i = 0; i < 4; i++) {
partList.add(new FilePart(name, FilePart()));
}*/

// 实例化上传数据的数组
Part [] parts = {new StringPart("user", name),new FilePart("file", new File(filePath))};

PostMethod filePost = new PostMethod(path);

filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

int status = client.executeMethod(filePost);
Log.i("Other", "返回结果:"+status);
if(status == 200){

System.out.println( filePost.getResponseCharSet());
String result = new String(filePost.getResponseBodyAsString());


System.out.println("--"+result);
return result;
}else{
throw new IllegalStateException("服务器状态异常");
}


}

}

----------------------------------------------------------------------------------------------------------

二、 以下是Activity中调用Service类的方法,进行批量上传图片

/**
* 批量上传图片
*/
private void filePostForSDPic() {
new Thread(){

public void run() {
try {
saveDir = Environment.getExternalStorageDirectory()+ "/chonPic";

String filePath = saveDir;
String path = "http://这里的路径就不贴上去了,保证成功并万无一失";
String result = DataService.sendDataByHttpClientPost(path, "admin", fileList);

Toast.makeText(MainActivity.this,"pic_haha:"+ result, 0).show();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};

}.start();
}

---------------------------------------------------------------------------------------

调用批量上传方法需要引入阿帕奇3 个jar包

1. commons-codec-1.3.jar

2. commons-httpclient-3.1.jar

3. commons-logging-1.1.jar

大家可以去网上下载,也可以去我的博客的资源下载!

更多相关文章

  1. Eclipse下搭建android开发环境
  2. katalon设置Android(安卓)SDK路径
  3. Android的FastBoot的安装与环境变量设置
  4. Weex 初体验(2)-navigator android跳转
  5. 如何把批量数据导入到android 的 sqlite 数据库
  6. Qt for Android(安卓)环境搭建方法
  7. android studio配置android开发环境
  8. android 百度地图集成sdk,混淆报错(最新)
  9. Android(安卓)Studio - local path doesn't exist

随机推荐

  1. Linux命令备忘实例(10)——目录管理
  2. linux下安装weblogic无图形化界面
  3. 鸟哥的linux私房菜学习笔记《三十九》Lin
  4. linux进程和线程排查 · 记一次JVM CPU高
  5. Linux服务器禁用ping
  6. linux 中 开放端口,以及防火墙的相关命令
  7. linux下如何杀掉D状态进程
  8. linux命令行程序
  9. 连接到Linux服务器时首先要运行的5个命令
  10. 读“rm -rf/ 又引发了一个血案”之触动