Answer the question
In order to leave comments, you need to log in
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'
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)
}
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question