Android FTP ChangeDirectory fail

482 Views Asked by At

I'm using ftp4j library to use FTPClient on android to upload text file from android to local network connected by Wifi.

If im not changing the directory then the default directory is storage/emulated/0

But if i try client.changeDirectory(Environment.getExternalStorageDirectory().getPath()) it catches Exception "failed to change directory". I want to upload the file from External Folder Directory such as DCIM, Downloads, Bluetooth folder (Just like when you browse your android from PC).

public void uploadFile(File fileName){        
    FTPClient client = new FTPClient();
    try {            
        client.connect(ftpHostname);
        client.login(ftpUsername, ftpPassword);
        client.setType(FTPClient.TYPE_BINARY);
        //client.changeDirectory(Environment.getExternalStorageDirectory().getPath());
        client.upload(fileName);
        notification = "The File Has Been Uploaded";
    } catch (Exception e) {         
        Log.e("UploadFTP", e.toString());
        notification = "Upload File Failed";

        try {
            client.disconnect(true);    
        } catch (Exception e2) {
            Log.e("UploadFTPDC", e2.toString());
        }
    } 
}

EDIT:

Seems like I'm confused with myself, After i log the getExternalStorageDirectory() it is returning /storage/emulated/0/ so no need to change the directory.

Now the ftp4j Exception changed to "could not create file"

11-14 10:59:24.936: E/UploadFTP(1764): it.sauronsoftware.ftp4j.FTPException [code=553, message= Could not create file.]

2

There are 2 best solutions below

0
On BEST ANSWER

You may have not write permission on the remote folder. Please check if the ftp user you logged in have a write permission on the directory. If not, give a write permission on it. If it unix like system try change permission like following command:

chmod +w folder
0
On

Don't you have to create the directory first ? Give this a try ...

client.createDirectory("/"+Environment.getExternalStorageDirectory().getPath()+"/");
client.changeDirectory("/"+Environment.getExternalStorageDirectory().getPath()+"/");

If this works for you please do tell.