2017-01-08 152 views
2

我需要在FTP中創建一個文件夾,並將此路徑「mnt/sdcard/yummycandy /」的所有文件上傳到此文件夾。FTP:創建文件夾(Android)

新文件夾的名稱必須是智能手機的序列號就像這個代碼:

public static String getManufacturerSerialNumber() { 
    String serial = null; 
    try { 
     Class<?> c = Class.forName("android.os.SystemProperties"); 
     Method get = c.getMethod("get", String.class, String.class); 
     serial = (String) get.invoke(c, "ril.serialnumber", "unknown"); 
    } catch (Exception ignored) {} 
    return serial; 
} 

這是我使用的代碼。我想你,請,對其進行修改(創建與設備的序列號命名的文件夾和上傳文件此文件夾中)

public static final String TAG = "Contacts"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Thread t = new Thread(new Runnable(){ 
     @Override 
     public void run(){ 
      jetzt(); 
     } 
    }); 
    t.start(); 
    Log.i(TAG, "thread started"); 

} 

public void jetzt() { 
    FTPClient ftpClient = new FTPClient(); 
    try { 
     ftpClient.connect(" HOST SERVER IP "); 

     ftpClient.setSoTimeout(10000); 
     ftpClient.enterLocalPassiveMode(); 
     if (ftpClient.login(" LOGIN ", " PASSWORD ")) { 
      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
      ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); 

      final File folder = new File("mnt/sdcard/gamouz"); 

      for (final File fileEntry : folder.listFiles()) { 
       try { 
        FileInputStream fs = new FileInputStream(fileEntry); 
        if (!fileEntry.isDirectory()) { 
         String fileName = fileEntry.getName(); 
         ftpClient.storeFile(fileName, fs); 
         fs.close(); 
         Log.i(TAG, "sent"); 
        } 
       } catch (Exception e) { 
        Log.i(TAG, "error uploading"); 
       } 
      } 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

請告訴我們您的代碼有什麼問題。 – Okas

+0

我希望你修改第二個代碼(創建一個以設備序列號命名的文件夾並在此文件夾中上傳文件) –

回答

2

使用FTPClient.makeDirectory

final String serial = getManufacturerSerialNumber(); 
ftpClient.makeDirectory(serial); 
ftpClient.changeWorkingDirectory(serial); 

(添加一些錯誤檢查)

+0

謝謝!你真棒 :) –