android使用异步http框架和第三方HttpClinet上传文件完整源码
首先我们先来看一下使用第三方HttpClinet的实现方式
这种实现方式需要用到一下3个jar包
commons-fileupload-1.2.1.jar 、 commons-io-1.4.jar 、 commons-httpclient-3.0.1.jar 。
这些jar已经包含在源码工程里边了
关于使用步骤我不多说,代码里边的注释已经写明
public void uploadByHttpClient(View view) {
String path = this.et_path.getText().toString().trim();
final String url = "http://110.65.99.66:8080/uploadfileServer/UploadFileServlet";
final File file = new File(path);
// 先判断文件是否存在
if (file.exists() && file.length() > 0) {
// 网络访问为耗时操作,应该放在子线程中执行
new Thread() {
public void run() {
// 1. 创建第三方HttpClient对象
HttpClient client = new HttpClient();
// 2. 创建POST请求对象
PostMethod post = new PostMethod(url);
// 3. 设置请求体内容
try {