Question

I am able to connect Ftp server. My requirement is to check if the path that will be shown after login into the server is writable in the ftp server. Below code is not checking File remotefile = new File(pwd)

public StringBuffer verifyMath(String host, String uname, String password, String cType){
    String MathString = "FTPHost:[" + host + "];uname[" + uname + "];cType[" + cType + "]";
    StringBuffer mBuffer = new StringBuffer();
    FileInputStream fis = null;
    FTPClient client = new FTPClient();

    try {
        client.connect(host);
        boolean login = client.login(uname, password);
        client.getReplyCode(); //230
        if (login == true) {
            log.debug("Connection established...");

            String pwd = client.printWorkingDirectory();
            File remotefile = new File(pwd);
            boolean rmtfile = remotefile.canWrite();
            boolean rmtdir = remotefile.isDirectory();

            if(!(remotefile.isDirectory() && remotefile.canWrite())) {
                mBuffer.append(MathLogger.raiseError(MathString, "Math is not Writable"));
            }           

            boolean logout = client.logout();
            if (logout) {
                log.debug("Connection close...");
            }
        } else {
            mBuffer.append(MathLogger.raiseError(MathString, "Connection failed."));
        }

    } catch (IOException e) {
        mBuffer.append(MathLogger.raiseError(MathString, e));
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return mBuffer;
}
Was it helpful?

Solution

mlistFile (or possibly mlistDir) is probably the API you are looking for to call on the remote directory. That returns an FTPFile object that has the permission info. Of course these will only work if the FTP server supports RFC 3659 extensions.

So something like:

FTPFile remoteDir = client.mlistFile(client.printWorkingDirectory());
if (remoteDir.hasPermission(FTPFile.USER_ACCESS,FTPFile.WRITE_PERMISSION)) {
  ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top