RSS订阅优然探索
你的位置:首页 » 未分类 » 正文

Android 文件上传

选择字号: 超大 标准 发布时间:2016-9-2 14:44:13 | 作者:admin | 0个评论 | 人浏览

 

package com.example.android_upload;

import java.io.File;

import org.apache.http.Header;
import org.apache.http.client.params.ClientPNames;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class MainActivity extends Activity {

 private EditText et_file;
 private ProgressBar progressBar1;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //获取控件
  et_file = (EditText) findViewById(R.id.et_upload);
  progressBar1= (ProgressBar) findViewById(R.id.progressBar1);
 }

 //点击上传按钮
 public void upload(View v) {
  int id = v.getId();
  switch (id) {
  case R.id.btn_upload:
   //获取上传文件的路径
   Toast.makeText(getApplicationContext(), "文件上传", 1).show();
   
   String path = et_file.getText().toString();
   //判断上次路径是否为空
   if (TextUtils.isEmpty(path.trim())) {
    Toast.makeText(this, "上次文件路径不能为空", 1).show();
   } else {
    //异步的客户端对象
    AsyncHttpClient client = new AsyncHttpClient();
    client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false); 
    //指定url路径
    String url = "http://192.168.0.118:9101/upload/?userid=qzf";
    url="http://192.168.0.133:8280/?userid=qzf";
    //url="http://192.168.0.122:8000/useraction/getsession";
    //封装文件上传的参数
    RequestParams params = new RequestParams();
    //根据路径创建文件
    File file = new File(path);
    try {
     //放入文件
     params.put("profile_picture", file);
    } catch (Exception e) {
     // TODO: handle exception
     System.out.println("文件不存在----------");
    }
    //执行post请求
    
    //client.addHeader("Content-Type", "text/xml; charset=utf-8");
    //client.addHeader("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4");
//    client.addHeader("Content-Type", "text/xml");
//    client.addHeader("Accept", "*/*"); 
//    client.addHeader("Accept-Encoding", "gzip, deflate"); 
//    client.addHeader("Accept-Language", "zh-cn"); 
//    client.addHeader("Connection", "Keep-Alive"); 
//    client.addHeader("Accept-Charset", "utf-8");
//    client.addHeader("Host", "192.168.0.191");
    //client.addHeader("Accept", "*/*"); 
    
//    headers.put("Accept", "*/*"); 
//          headers.put("Accept-Encoding", "gzip, deflate"); 
//          headers.put("Accept-Language", "zh-cn"); 
//          headers.put("Connection", "Keep-Alive"); 
    client.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");
    client.post(url,params, new AsyncHttpResponseHandler() {

     
     
     @Override
     public void onProgress(int bytesWritten, int totalSize) {
      // TODO Auto-generated method stub
      
      super.onProgress(bytesWritten, totalSize);
      progressBar1.setProgress(bytesWritten*100/totalSize);
     }

     @Override
     public void onSuccess(int statusCode, Header[] headers,
       byte[] responseBody) {
      
      String str = new String(responseBody);
      if (statusCode == 200) {
       System.out.println(str);
       Toast.makeText(getApplicationContext(), "上次成功"+ str.toString(), 1).show();
      }
      else
      {
       Toast.makeText(getApplicationContext(), "上传失败"+statusCode, 1).show();
      }
     }

     @Override
     public void onFailure(int statusCode, Header[] headers,
       byte[] responseBody, Throwable error) {
      
      //error.printStackTrace();
      //String str = new String(responseBody);      Toast.makeText(getApplicationContext(), "onFailure上传失败"+statusCode, 1).show();
     }
    });

   }
   break;

  default:
   break;
  }

 }

}

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。