android 使用http協議上傳文件 – Android移動開發技術文章_手機開發 Android移動開發教學課程

補充: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.aiwalls.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的專欄

 

 

發佈留言