Upload directory to FTP ( Android )

2.8k views Asked by At

I want to upload directory to my FTP server.

This is the code that i use to upload a "single file" to my ftp server, I want to know if there is something that can help me to upload all pictures ".jpg" in this folder "mnt/sdcard/DCIM/Camera" to my FTP.

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);
            File sFile=new File("mnt/sdcard/DCIM/Camera/mypicture.jpg");
            FileInputStream fs= new FileInputStream(sFile);
            String fileName = sFile.getName();
            Boolean result = ftpClient.storeFile(fileName, fs);
            fs.close();
            Log.i(TAG, "sent");
            String has = "";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

2

There are 2 answers

0
ashubuntu On BEST ANSWER

You may use this modified function:

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/DCIM/Camera");

            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();
    }
}
2
Martin Prikryl On

There's no function to upload whole directory in the FTPClient class.

You have to: