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.

msrd0
  • 7,816
  • 9
  • 47
  • 82
BigZee
  • 456
  • 5
  • 22
  • 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 '18 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 '18 at 14:16

1 Answers1

5

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()
}
msrd0
  • 7,816
  • 9
  • 47
  • 82
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • 2
    @ZararYounis That code should not produce any syntax errors, did you add the library to your gradle dependencies? – msrd0 May 12 '18 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 '18 at 14:13
  • there is this tho, but the app just closes ? https://stackoverflow.com/questions/47424402/how-bypass-networkonmainthreadexception-on-kotlin – Datadimension Jun 06 '19 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 05 '23 at 10:34