D
D
Dmtm2019-05-09 16:04:30
Android
Dmtm, 2019-05-09 16:04:30

Why does ftpClient.changeWorkingDirectory sometimes fail even though there are no errors?

apache library is used

implementation  group: 'commons-net', name: 'commons-net', version: '3.6'
    implementation  group: 'commons-io', name: 'commons-io', version: '2.6'

when quickly moving through folders, sometimes dir==null, and when you re-enter, it most often works correctly, while the cycle has been added, but this is of course not a solution
. neither Exception nor isPositiveCompletion work
, and the list of files also sometimes turns out to be empty (after changing the folder correctly)
suspend fun changeDir(ftpClient: FTPClient, targetDir: String): String {
            var i = 0
            var dir: String? = null
            do {
                if (!ftpClient.changeWorkingDirectory(targetDir) || !FTPReply.isPositiveCompletion(ftpClient.replyCode)) {
                    throw Exception(ftpClient.replyString)
                }
                dir = ftpClient.printWorkingDirectory()
                Log.d(TAG, "changeDir completed, current: " + dir)
            } while (i++ < 3 && dir == null)

            return targetDir
        }

        /**
         * @param ftpClient ftpClient in connected state
         * */
        suspend fun fileList(ftpClient: FTPClient): List<FTPFile> {
            val fileList = ftpClient.listFiles()//App.instance.ftpFolderNavigator.buildPath()
            Log.d(TAG, "fileList from: " + ftpClient.printWorkingDirectory())
            return if (FTPReply.isPositiveCompletion(ftpClient.replyCode)) {
                Log.d(TAG, "size: " + fileList.size)
                fileList.asList()
            } else {
                throw Exception("Server file list taken error, code: " + ftpClient.replyCode)
            }
        }

the connection is opened like this:
suspend fun connect(server: String, port: Int, login: String, pass: String): FTPClient {
            val ftpClient = FTPClient()
            ftpClient.autodetectUTF8 = true
            ftpClient.controlEncoding = "UTF-8";
            ftpClient.enterLocalPassiveMode()
            ftpClient.connect(server, port)
            if (!ftpClient.login(login, pass)) {
                throw Exception("Login error")
            }

            if (FTPReply.isPositiveCompletion(ftpClient.replyCode)) {
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            } else {
                throw Exception("Server connection error, code: " + ftpClient.replyCode)
            }
            return ftpClient
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmtm, 2019-05-14
@Dmtm

rewritten to ftp4j, it works
PS: and this is not the first time there are problems with apache libraries

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question