Problem trying to transfer files using the Apache FTP implementation in Java

1

I am performing FTP file transfer using JAVA. I'm using Apache's FTPClient and FTPServer classes. In a specific environment, sometimes the files are not transferred. I call the enterLocalPassiveMode method of FTPClient before calling the login method and even then sometimes the file is not transferred.

The storeFile method of FTPCLient returns "false". The getReplyString method of the FTPClient returns "200 Command TYPE okay". The list method of FTPClient returns "227".

When the file is successfully transferred the returns are as follows: The list method of FTPCLlient returns 150. The getReplyString method of the FTPClient returns "150 File status okay; about to open data connection".

Is there a Firewall problem?

I tried to use passive port range in the FTPServer using the setPassivePorts method of the DataConnectionConfigurationFactory class, but the problem continues.

Is there any way to set a range of ports on the client side? How can I check if the connection is actually using passive mode?

Thanks in advance.

    
asked by anonymous 12.07.2016 / 21:10

1 answer

2
  

Is there a Firewall problem?

I could not be sure. The correct would be to unlock the firewall or open an exception for the application. But neither option was possible. The fact is that in the development environment does not give any problem, but in the production environment occurs. What is probably happening is the firewall blocking the ports.

  

Is there any way to set a range of ports on the client side? How can I check if the connection is actually using passive mode?

You can set the port that will be used by the client in the FTPClient connect () method itself:

this.getFtpClientUtil().connect(settingsService.getServer(), settingsService.getFtpServerCommandChannelPort(), 
                InetAddress.getLocalHost(), getLocalPort());

Upon verifying that the connection is using passive mode, the FTPClient's getPassivePort () method will return -1 if the passive connection is not being used. In the question, I commented that when the file is not sent successfully, the FTPClient's list () method returns the code 227. This code is also a clue that the connection is in passive mode. I was wondering if this was the error code, but in fact something went wrong after entering passive mode and the reply from the previous command was returned, which in this case is 227.

While still configuring the local port, the problem continued. Maybe because the local port that I'm configuring is only for the command channel. The ports of the transfer channel remain random. The solution was to try to download the file again. If the file transfer fails, I disconnect the client, reconnect using other ports and try downloading again. In all the tests that I did, the second attempt was successful. I put it to try only 3 times.

When connecting again, if the same port is used, a "java.net.BindException: Address already in use" error may be thrown. Even after disconnecting the client, the door may be stuck for a while. In this case, I check beforehand if the port is in use, and if so, I try to connect the client to another port.

    
27.07.2016 / 14:26