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.

JScape

  • 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.

Zehon

  • 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.

JCraft JSch

  • 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: , , , , , , , , , ,
This entry was posted on Monday, March 1st, 2010 at 8:44 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

12 Responses to “Which library to use for FTP, SFTP, FTPS file transfer in Java?”

  1. Christian

    Hi,

    apache commons net might not support sftp but apache commons vfs does… http://commons.apache.org/vfs/filesystems.html#SFTP

  2. Tim

    Apache’s net 2.2 does SFTP. The 1.4 does not.

  3. Gustavo

    Apache’s net 3.0.1 does FTPS but not SFTP, they’re not the same. The first uses SSL whereas the second SSH.

  4. Alex

    Just a small note, FileZilla does not support SFTP either. On windows you’ll have to find a different server. Solaris on the other hand supports SFTP out of the box but I haven’t found anything yet that says whether or not it supports FTPS in the same manner. Great post by the way, summed the answers for a lot of the questions I had for FTP up well in a single response.

  5. Onkar Joshi

    Pleasure to have been of help. :)

  6. Hemant

    While using Apache Common Net ‘s FTPSClient, the process gets hang over “connect” method call.

    Any idea why is it so!!!

  7. Onkar Joshi

    @Hemant
    There could be many reasons.

    Look out for firewall issues.

    Try connecting with a well known client such as FileZilla to see if things work fine.

  8. Sunil

    SFTP is supported by FileZilla, only the thing is you need to create profile for login.

  9. Shannara

    Any other can be used? Zehon is practically unusable for SFTP, same with the underline apache libraries. JCraft libraries do not contain any working samples/code.

    All that is left is JScape, though it is not free.

    It looks like there are no free way to SFTP in java?

  10. Michal

    No, there is one Apache Camel-FTP, which uses commons-net for FTP and FTPS connections and JCraft JSCH for SFTP.

  11. gitcraker

    For SFTP, use jcraft api. I have used it many times and it works.

    For FTPS, I will go with Onkar’s recommendation :)

  12. Shrikant

    Hey Onkar, I decided to use Zehon SFTP for java.
    I interface with Globalscape EFT server, which Zehon mentions on their site as a recommended server.
    however,I am running into a status code 530-not-logged-in on Globalscape eft server,when I run my batch process to pickup/drop files using zehon.
    can any one suggest what approach I need to take in such a case?
    have written to globalscape on this issue,though.

Leave a Reply

Your comment