I'm transferring files using FTP with JAVA. I'm using FTPClient and FTPServer from Apache. But in a specific environment, sometimes the file isn't transferred. I call the enterLocalPassiveMode method from FTPClient before the login method call and yet sometimes the files aren't transferred.
- The storeFile method returns "false".
- The getReplyString method returns "200 Command TYPE okay".
- The list method returns "227".
When the file is transferred successfully:
- The list method returns 150.
- The getReplyString method returns "150 File status okay; about to open data connection".
Client code:
this.getFtpClientUtil().connect(settingsService.getServer(), settingsService.getFtpServerCommandChannelPort());
int reply = this.getFtpClientUtil().getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
logger.error("Fail to connect to FTP Server");
this.getFtpClientUtil().disconnect();
isConnect = false;
return false;
}
logger.info("FTP Server connected successfully");
this.getFtpClientUtil().enterLocalPassiveMode();
isConnect = this.getFtpClientUtil().login(settingsService.getftpUsername(), settingsService.getftpPassword());
Server Code:
// Data Configuration
this.dataConfigurationFactory = new DataConnectionConfigurationFactory();
dataConfigurationFactory.setPassiveAddress(this.ipAddress);
dataConfigurationFactory.setPassiveExternalAddress(this.ipAddress);
dataConfigurationFactory.setPassivePorts(this.settingsService.getFtpServerTransferChannelPort());
// Listener
listenerFactory = new ListenerFactory();
listenerFactory.setPort(this.settingsService.getFtpServerCommandChannelPort());
listenerFactory.setDataConnectionConfiguration(dataConfigurationFactory.createDataConnectionConfiguration());
// Desliga SSL
listenerFactory.setImplicitSsl(false);
// User
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
UserManager um = userManagerFactory.createUserManager();
this.serverFactory = new FtpServerFactory();
serverFactory.addListener("default", listenerFactory.createListener());
serverFactory.setUserManager(um);
UserFactory userFactory = new UserFactory();
userFactory.setName(this.settingsService.getftpUsername());
userFactory.setPassword(this.settingsService.getftpPassword());
userFactory.setMaxIdleTime(60);
final String absolutePath = this.settingsService.getftpHomeDirectory();
userFactory.setHomeDirectory(absolutePath);
userFactory.setAuthorities(Arrays.asList((Authority) new WritePermission()));
User user = userFactory.createUser();
um.save(user);
// Connection Config
connectionConfigFactory = new ConnectionConfigFactory();
connectionConfigFactory.setAnonymousLoginEnabled(false);
connectionConfigFactory.setMaxLogins(100);
serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
Is this a firewall problem?
I tried set passive port ranges on the server side using the setPassivePorts method from DataConnectionConfigurationFactory class, but the problem continues.
Is there any form to set port ranges on the Client side? How can I check if the connection is really using passive mode?
Thanks in advance.