15

I am writing a JAVA code to traverse FTP Location using Apache Commons Net FTPClient and getting output in an Excel file. the code execute correctly for approx 5 min but then gives an IOException:

org.apache.commons.net.ftp.FTPConnectionClosedException: FTP response 421 received.  Server closed connection.

I am using commons-net-3.0.1.jar. I have done some R&D and tried:

setDefaultTimeout(6000); 
setConnectTimeout(3000);
setSoTimeout(3000);
enterLocalPassiveMode();

and sending NOOP, but still getting the same error.

All I am trying to do is traverse through a directory and if file is found than get file name and file update date in excel else if directory is found then get inside and do until file is found again.

Please help and ask if any other information is required. I am new to JAVA.

Castaglia
  • 2,972
  • 5
  • 28
  • 49
crazyproton
  • 163
  • 1
  • 1
  • 8

2 Answers2

19

See here: http://kb.globalscape.com/KnowledgebaseArticle10142.aspx

    Error 421 Service not available, closing control connection. 
    Error 421 User limit reached 
    Error 421 You are not authorized to make the connection 
    Error 421 Max connections reached 
    Error 421 Max connections exceeded 

Maybe you're not reusing a connection but using a new connection for every request, flooding the server with connections until it hits the connection limit. Try closing your connections or resuing them.

lfurini
  • 3,729
  • 4
  • 30
  • 48
fiffy
  • 840
  • 9
  • 16
  • Yer you were right. I was exhausting the connection but now I reused the connection and it worked. Also thanks for providing the link. It is really helpful outside this question as well. Thanks – crazyproton Jul 17 '14 at 11:23
  • YES! This worked for me as well. I'm using Cyberduck on OS X, within Preferences > Transfers > General > Transfer Files there is an option to 'Open Single Connection'. Switching to this mode works and my files are now uploading properly. – James Mar 30 '19 at 15:15
  • Thanks, it helps me much. I was trying to connect multiple FTP connections at a time. That's why it returns 421 responses. Now after closing all connections and connect again. Now it is working fine. – Ariful Islam May 05 '21 at 18:35
3

For future reference..

If the solution by @fiffy didn't work, maybe try turning on the TLS (FTPS/Secure Connection), My server was set to only accept FTPS protocol, so it was rejecting my unencrypted connection, so turning on the TLS (FTPS) helped me solve the prob.

Note:- This error FTP response 421 received is very prominent in Netbeans since its also built in java.

Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88