javaandroidftpapache-commons-net

FTP: Create folder (Android)


I need to create a folder in FTP and upload all files of this path "mnt/sdcard/yummycandy/" to this folder.

The name of the new folder must be the Serial number of the Smartphone like in this code :

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;
}

This is the code that i use. I want you, please, to modify it (creating a folder named with serial number of device and upload files in this folder)

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();
    }
}

Solution

  • Use the FTPClient.makeDirectory:

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

    (add some error checking)