标签:android style http io os 使用 java ar for
import java.io.File; import java.nio.charset.Charset; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import com.lxb.uploadwithprogress.http.CustomMultipartEntity.ProgressListener; /** * Multipart上传(需要httpmime.jar) */ public class HttpMultipartPost extends AsyncTask<String, Integer, String> { private static final String TAG = HttpMultipartPost.class.getSimpleName(); private Context context;//上下文 private List<String> filePathList;//上传文件列表 private ProgressDialog pd;//对话框 private long totalSize;//总大小 public HttpMultipartPost(Context context, List<String> filePathList) { this.context = context; this.filePathList = filePathList; } @Override protected void onPreExecute() { pd = new ProgressDialog(context); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("上传中..."); pd.setCancelable(false); pd.show(); } @Override protected String doInBackground(String... params) { String result = null; HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost("URL"); try { //监听上传进度 CustomMultipartEntity multipartContent = new CustomMultipartEntity( new ProgressListener() { @Override public void transferred(long num) { publishProgress((int) ((num / (float) totalSize) * 100)); } }); //把上传内容添加到MultipartEntity for (int i = 0; i < filePathList.size(); i++) { multipartContent.addPart("file", new FileBody(new File(filePathList.get(i)))); multipartContent.addPart("data", new StringBody(filePathList.get(i), Charset.forName(org.apache.http.protocol.HTTP.UTF_8))); } totalSize = multipartContent.getContentLength(); Log.i(TAG,"totalSize: " + totalSize); httpPost.setEntity(multipartContent); HttpResponse response = httpClient.execute(httpPost, httpContext); result = EntityUtils.toString(response.getEntity()); Log.i(TAG,"result: " + result); } catch (Exception e) { e.printStackTrace(); } return result; } @Override protected void onProgressUpdate(Integer... progress) { pd.setProgress((int) (progress[0])); } @Override protected void onPostExecute(String result) { Log.i(TAG,"result: " + result); pd.dismiss(); } @Override protected void onCancelled() { Log.i(TAG,"onCancelled..."); } }
import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; /** * CustomMultipartEntity.classs 用于监听和封装Filebody参数 */ public class CustomMultipartEntity extends MultipartEntity { private final ProgressListener listener; public CustomMultipartEntity(final ProgressListener listener) { super(); this.listener = listener; } public CustomMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener) { super(mode); this.listener = listener; } public CustomMultipartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) { super(mode, boundary, charset); this.listener = listener; } @Override public void writeTo(OutputStream outstream) throws IOException { super.writeTo(new CountingOutputStream(outstream, this.listener)); } public static interface ProgressListener { void transferred(long num); } public static class CountingOutputStream extends FilterOutputStream { private final ProgressListener listener; private long transferred; public CountingOutputStream(final OutputStream out, final ProgressListener listener) { super(out); this.listener = listener; this.transferred = 0; } public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } public void write(int b) throws IOException { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } }
使用方法
List<String> filePathList = new ArrayList<String>(); filePathList.add(Environment.getExternalStorageDirectory()+ "/baidu/cache/image/" + "12778991134.png"); filePathList.add(Environment.getExternalStorageDirectory()+ "/baidu/cache/image/" + "123457894146.png"); HttpMultipartPost httpPost = new HttpMultipartPost(Acitivity,filePathList); httpPost.execute();
标签:android style http io os 使用 java ar for
原文地址:http://my.oschina.net/oppo4545/blog/315420