Search code examples
javaapache-commonsnatftps

JAVA FTPSClient file listing issue


I am using apache commons-net 3.6 library to connect FTPS server. FTPS server is behind NAT of thirdparty. and I can't change any settings on server side. I can login to server, but can not list files. I've tried same code with some public FTP and FTPS servers, and result was successfull. Seems that they are not behind NAT. But filezilla can successfully connect and list files from my problematic server. There is my code

       ftps.connect(server, port);
       System.out.println("Connected to " + server + ".");

        reply = ftps.getReplyCode();
        ftps.enterLocalPassiveMode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftps.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
        if (!ftps.login(username, password)) {
            ftps.logout();

        }
      //  ftps.setEnabledSessionCreation(false);
        ftps.feat();
        ftps.execPBSZ(0);
        ftps.execPROT("P");
        ftps.setFileType(FTP.BINARY_FILE_TYPE);  
        FTPFile dirs[] = ftps.listDirectories();

And there is my ftps log:

220 FTP Server ready.
AUTH TLS
234 AUTH TLS successful
Connected to x.x.x.x
USER *******
331 Password required for azercell
PASS *******
230 User myuser logged in
FEAT
211-Features:
 MDTM
 MFMT
 LANG bg-BG;en-US;fr-FR;it-IT;ja-JP;ko-KR;ru-RU;zh-CN;zh-TW
 TVFS
 UTF8
 AUTH TLS
 MFF modify;UNIX.group;UNIX.mode;
 MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*;
 PBSZ
 PROT
 REST STREAM
 SIZE
211 End
PBSZ 0
200 PBSZ 0 successful
PROT P
200 Protection set to Private
TYPE I
200 Type set to I
SYST
215 UNIX Type: L8
PASV
227 Entering Passive Mode (192,168,2,50,192,12).
[Replacing PASV mode reply address 192.168.2.50 with x.x.x.x]
LIST
150 Opening BINARY mode data connection for file list
425 Unable to build data connection: Operation not permitted

I'd read that prior to version 3.6 commons-net library prior couldnt handle behind NAT connections properly.

Can anyone help me? What is wrong with my code?


Solution

  • So my conclusion is problem was not related to NAT technology, apache-commons 3.6 does not handle all FTPS options properly. As I mentioned before we were integrating with 3rd party and had not option to change FTPS settings, at least we installed filezilla ftp server and were able to reproduce error. Fortunately I found solution at http://eng.wealthfront.com/2016/06/10/connecting-to-an-ftps-server-with-ssl-session-reuse-in-java-7-and-8/ by Luke Hansen. Great thanks him