0

https://medium.com/nerd-for-tech/retrieving-files-from-ftp-server-using-spring-integration-5ccc4a972eaf

I'm implementing FTP behavior based on the above site. In the above site, I've only added passive mode to the ftp settings.

@Bean
public DefaultFtpSessionFactory sf() {
    DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();

    sf.setHost(host);
    sf.setPort(port);
    sf.setUsername(username);
    sf.setPassword(password);
    sf.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
    return sf;
}

And I want to check the passive port when communicating in practice.

@ServiceActivator(inputChannel = "ftpMGET")
@Bean
public FtpOutboundGateway getFiles() {
    DefaultFtpSessionFactory ftpSessionFactory = sf();
    FtpOutboundGateway gateway = new FtpOutboundGateway(ftpSessionFactory, "mget", "payload");
    gateway.setAutoCreateDirectory(true);
    gateway.setLocalDirectory(new File(downloadPath));
    gateway.setFileExistsMode(FileExistsMode.APPEND);
    gateway.setFilter(new AcceptOnceFileListFilter<>());
    gateway.setOutputChannelName("fileResults");
    gateway.setOption(AbstractRemoteFileOutboundGateway.Option.RECURSIVE);

    System.out.println(ftpSessionFactory.getSession().getClientInstance().getPassivePort());
    return gateway;
}

At the end, I added a passivePort with System.out.println.

It keeps saying -1, but how do I check the passive port when downloading a file from a real server?!

anna
  • 1
  • 1
  • I've answered your question (or did I?). But this looks bit like [XY problem](https://meta.stackexchange.com/q/66377/218578), as your check makes little sense to me. Maybe you should better explain us what is it that you are trying to achieve. – Martin Prikryl Jul 17 '22 at 07:49

1 Answers1

0

The FTPClient.getPassivePort cannot return any actual value, before you transfer anything. Each transfer uses a different (passive) port. Until you actually transfer anything, no passive port is involved.

As the documentation of the FTPClient.getPassivePort method says:

This method only returns a valid value AFTER a data connection has been opened after a call to enterLocalPassiveMode(). This is because FTPClient sends a PASV command to the server only just before opening a data connection, and not when you call enterLocalPassiveMode().

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992