当前位置:操作系统 > 安卓/Android >>

android 使用http协议上传文件

补充:android平台调用post方法进行文件传输:


public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        new Thread(new Runnable(){ 
        public void run() { 
            String actionUrl = "http://202.201.12.19/uploadFile.php"; 
                Map<String, String> strParams = new HashMap<String, String>(); 
                strParams.put("paramOne", "valueOne"); 
                strParams.put("paramTwo", "valueTwo"); 
                Map<String, File> files = new HashMap<String, File>(); 
                files.put("tempAndroid1.txt", new File("/mnt/sdcard/HelloWorld1.txt")); 
                try{ 
                    Log.i("xiao", "post Result = " + post(actionUrl, strParams, files)); 
                } 
                catch(Exception e){ 
                    Log.i("xiao", "Exception = " + e.getMessage()); 
                } 
        } 
        }).run(); 
    }  www.zzzyk.com
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new Thread(new Runnable(){
  public void run() {
   String actionUrl = "http://202.201.12.19/uploadFile.php";
          Map<String, String> strParams = new HashMap<String, String>();
          strParams.put("paramOne", "valueOne");
          strParams.put("paramTwo", "valueTwo");
          Map<String, File> files = new HashMap<String, File>();
          files.put("tempAndroid1.txt", new File("/mnt/sdcard/HelloWorld1.txt"));
          try{
           Log.i("xiao", "post Result = " + post(actionUrl, strParams, files));
          }
          catch(Exception e){
           Log.i("xiao", "Exception = " + e.getMessage());
          }
  }
        }).run();
    }

注意:我服务器端(linux)的代码和转载的博客写的一样,但总是不能进行传输,因为服务器端我没有对当前文件夹进行写入的权限,进入/var/www目录,加上chmod  777 .就可以了。

 摘自 xiaoxiaobian3310903的专栏

 


 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,