Problem trying to transfer files using Apache FTP implementation in Java

Asked

Viewed 162 times

1

I am performing file transfer via FTP using JAVA. I’m using the classes Ftpclient and Ftpserver apache. In a specific environment, sometimes files are not transferred. I call the method enterLocalPassiveMode of Ftpclient before calling the method login and even then sometimes the file is not transferred.

The method storeFile of Ftpclient returns "false". The method getReplyString of 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 method list of Ftpcllient returns 150. The method getReplyString of Ftpclient returns "150 File status okay; about to open data Connection".

Is some Firewall problem?

I tried to use passive port range on Ftpserver using the method setPassivePorts class DataConnectionConfigurationFactory, but the problem remains.

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

From now on, thank you.

  • Welcome to SOPT. Would have as [Edit] your post and add what language is using, and if possible as this your code?

  • 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.

1 answer

2


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.