3

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.

2
  • 2
    FTP 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)
    – msrd0
    May 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
    – BigZee
    May 13, 2018 at 14:16

1 Answer 1

5
+50

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()
}
4
  • 2
    @ZararYounis That code should not produce any syntax errors, did you add the library to your gradle dependencies?
    – msrd0
    May 12, 2018 at 15:16
  • 1
    yeah I corrected it and its working now thanks but I ended up using apache commons net I will upload the code soon
    – BigZee
    May 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.
    – spiegelm
    Jan 5 at 10:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.