2012-06-09 37 views
-1

我正在開發一個FTP項目,以將相機拍攝的圖片發送到FTP服務器。如何更改我可以訪問特定文件夾的ftp​​代碼

現在,它是可能的,我picuture將保存在文件夾中:/ SD卡/ FTP/

我怎麼也得將代碼從我FTPActivity改變,該文件夾中的文件將被髮送?

希望有人能給我一個例子。或發佈更改後的代碼。這對我很有幫助。

這裏我的FTP客戶端,代碼:

package de.android.datenuebertragung; 

import java.io.ByteArrayInputStream; 
import java.io.IOException; 

import org.apache.commons.net.ftp.FTPClient; 

import android.app.Activity; 
import android.util.Log; 

public class FTPManager extends Activity{ 
    FTPClient con = new FTPClient();{ 


    try 
    { 
     con.connect("host"); 
     if (con.login("user", "password")) 
     { 

      con.enterLocalPassiveMode(); // important! 
      String data = "Test 09.06.2012"; 
      ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); 
      boolean result = con.storeFile("/FTPTest.txt", in); 
      in.close(); 
      if (result) Log.v("upload result", "succeeded"); 
      System.out.println("Test ok ..."); 


     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 


    try 
    { 
     con.logout(); 
     con.disconnect(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    }} 

回答

0

這是僞代碼。我還沒有測試過這個問題的語法或錯誤。

// in application never use hardcoded paths 
File folder = new File("/sdcard/ftp/"); 
File tempFile = null; 
FTPFile[] tempFTPFile = null; 

//get all files in folder 
for(String fileInDir : folder.list()){ 
    tempFile = new File(fileInDir); 
    tempFTPFile = ftpConnection.listNames(tempFile.getName); 

    if(tempFTPFile!=null || tempFTPFile.length<=0){ 
     ftpConnection.upload(fileInDir); 
    } 
} 
+0

謝謝。但是,我應該在哪裏插入這部分代碼?在Avtivity的開始?對不起,我是Android編程的絕對新手。 – goekie

相關問題