最近手头上需要批量上传一些保存到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
大家可以去网上下载,也可以去我的博客的资源下载!