Is it possible to use Ktor to upload files via FTP? Or is it possible in Kotlin by default?
Current version without Ktor:
implementation 'commons-net:commons-net:3.9.0'
import org.apache.commons.net.ftp.FTP
import org.apache.commons.net.ftp.FTPClient
import java.io.File
import java.io.FileInputStream
fun uploadFileToFtp(file: File, ftpServer: String, ftpUsername: String, ftpPassword: String, ftpDirectory: String) {
val ftpClient = FTPClient()
ftpClient.connect(ftpServer)
ftpClient.login(ftpUsername, ftpPassword)
ftpClient.enterLocalPassiveMode()
ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
ftpClient.changeWorkingDirectory(ftpDirectory)
val inputStream = FileInputStream(file)
val fileName = file.name
ftpClient.storeFile(fileName, inputStream)
inputStream.close()
ftpClient.logout()
ftpClient.disconnect()
}
1 Like