Which library to use for FTP, SFTP, FTPS file transfer in Java?
Recently, I was writing code to transfer data to third party servers. In some cases my interface to them was SFTP (Secure FTP) and in others FTPS (FTP-SSL). And existing coding for one of these really didn’t take care of the other. I evaluated and tested a few different libraries to see which would work well for us. Here are a few quick notes on a few of them.
- I read about quite a few people recommending this as the quality solution.
- This isn’t free in terms of $$$.
- If file transfer is important to you and you are willing to pay for it, you may want to go with JScape.
- The Zehon libraries have support for FTP, SFTP and FTPS.
- They offer a very simple interface in the form of a bunch of static methods to do actions like sending and receiving files.
- No source code is provided.
- Can be difficult to debug in case of problems.
- They do use Apache Commons Net as a base but some of the power is hidden by Zehon in exchange for simplicity.
Apache Commons Net
- Using this directly offers greater power and more complete control over the protocol features compared to Zehon.
- For a quick start, see sample code in “examples.FTPExample” provided with the Apache Comons Net source distribution.
- No support for SFTP.
- Widely used for SFTP.
- “JSch is a pure Java implementation of SSH2.”
Here are some issues you may run into -
- You may need to pick up some understanding of “Impicit SSL” and of the “PASV” (passive) mode to deal with firewall issues.
- For Zehon, I had to specify a keystore it could use. Apache picked up the default java certs keystore with its default configuration.
- I believe the core Apache Commons Net library (and hence Zehon too) has some kind of a caching/flushing bug. When trying to transfer very small files of size 1 byte to 175 bytes or so, the library reported the file to have been transferred successfully – but it actually resulted in a zero byte file being created at the target location. Beware! I’ll write about how I fixed this in a different post. I had to add a bit of code in the Apache Commons Net sources and use my own compiled jar for it to work well.
- If you are really doing security correctly – you will need to know how to use keytool and how to handle your certificates correctly.
- You can use the free FileZilla FTP Server to start an FTP server on your own machine for development and testing.
Tags: apache, ftp, ftps, jcraft, jscape, keystore, keytool, sftp, ssh, ssl, zehon