Is some Firewall problem?
I couldn’t be sure. The correct thing would be to unblock the firewall or make an exception for the application. However, neither of the two options were possible. The fact is that in the development environment gives no problem, but in the production environment occurs. What is probably happening is the firewall blocking the doors.
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?
It is possible to set the port that will be used by the client in the connect() method of Ftpclient itself:
this.getFtpClientUtil().connect(settingsService.getServer(), settingsService.getFtpServerCommandChannelPort(),
InetAddress.getLocalHost(), getLocalPort());
On whether the connection is using passive mode, the Ftpclient getPassivePort() method will return -1 if the passive connection is not being used. In the question, I commented that when the file is not successfully uploaded, the Ftpclient list() method returns code 227. This code is also an indication that the connection is in passive mode. I was imagining that this was the error code, but actually some problem occurred after going into passive mode and the reply of the previous command was returned, which in this case is the 227.
Despite also setting up the local port, the problem continued. Perhaps because the local port I am configuring is only for the command channel. The transfer channel ports are still random.
The solution was to try to transfer the file again. If the file transfer fails, I disconnect the client, connect again using other ports and try to transfer again. In all the tests I did, on the second try the file is transferred successfully. I put 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 released. Even after disconnecting the client, the door can be trapped for a while. In this case, I check before if the port is in use, and if so, I try to connect the client to another port.
Welcome to SOPT. Would have as [Edit] your post and add what language is using, and if possible as this your code?
– David
Thank you, David. I have detailed the problem. Putting the code is a little more complicated, but if it is not possible to understand the description I will see what can be put.
– Fagner Fonseca