I am trying to create an app that needs to upload some images and text files to my server which supports FTP. I am from a python background and have used FTP a lot in the past but I am pretty confused while using it in Kotlin any help will be appreciated.
-
2FTP is an old, unsafe and inperformant protocol, especially because it opens a new port for every file transfer. Consider using alternatives like SFTP (not to be confused with FTPS)– msrd0May 12, 2018 at 15:14
-
thanks for the suggestion but my server only supports FTP (its kind of a headache )and I want to use it so I am kinda stuck with FTP– BigZeeMay 13, 2018 at 14:16
Add a comment
|
1 Answer
You can use ftp4j library by download ftp4j-1.7.2.jar.
Here is sample code in kotlin.
try {
val mFtpClient = FTPClient()
mFtpClient.connect("hots", PORT)
mFtpClient.login("user", "password")
mFtpClient.type = FTPClient.TYPE_BINARY
mFtpClient.changeDirectory("/directory_path/")
mFtpClient.upload(File("file_path"))
mFtpClient.disconnect(true)
} catch (e: Exception) {
e.printStackTrace()
}
-
2@ZararYounis That code should not produce any syntax errors, did you add the library to your gradle dependencies?– msrd0May 12, 2018 at 15:16
-
1yeah I corrected it and its working now thanks but I ended up using apache commons net I will upload the code soon– BigZeeMay 13, 2018 at 14:13
-
there is this tho, but the app just closes ? stackoverflow.com/questions/47424402/… Jun 6, 2019 at 19:23
-
Found this in my google search. Please note that ftp4j looks unmaintained. The last version 1.7.3 on GitHub is from 2015 and there hasn't been any activity since. I wouldn't recommend using outdated libraries.– spiegelmJan 5 at 10:34