3

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?

ali
  • 145
  • 14
  • It can be a stupid answer but you have wrote that " But filezilla can successfully connect and list files " so you can see files, not directory, maybe the directory is private but not the files, so try FTPFile[] files1 = ftps.listFiles(); – Liz Lamperouge Apr 20 '17 at 09:04
  • LizLamperouge, I've tried it before. same error – ali Apr 20 '17 at 09:39
  • ok, are you sure that you are in the correct directory? try to ftps.printWorkingDirectory(); ftps.changeWorkingDirectory("/directory"); – Liz Lamperouge Apr 20 '17 at 12:08
  • Yes. İ am sure. İ tried but is not working – ali Apr 20 '17 at 13:40
  • It is very strange, I have exactly the same code as yours, but in other order, but I don't thing is this the problem; I have also see that I use apache commons-net 3.4 instead of 3,6. Try it – Liz Lamperouge Apr 20 '17 at 13:54
  • İ can easily connect to public servers and list files. İ think problem with servers which are behind NAT – ali Apr 21 '17 at 05:03
  • Any other suggestions? – ali Apr 25 '17 at 05:41

1 Answers1

3

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

ali
  • 145
  • 14
  • This is a really crazy answer and I don't really understand how I thelp. I don't see any of the errors listed in this tutorial. It just returns an empty array for me. – Thomas Beauvais Mar 26 '22 at 16:53
  • If you an access try to change FTPS settings, or may be you hav newer java version – ali Mar 28 '22 at 06:21