5

I have a given FTP Server. I can connect to the server per WinSCP and other programs. I had allready a connection via Java to the server but after a while I can not connect. Per WinSCP I can connect the whole time. Now there is the question if the server is the reason of the problem or the program.

There is my Code:

private FTPClient ftpClient = null;

public FtpServerConnector() throws Exception {
    ftpClient = new FTPClient();
    ftpClient.connect(url);
    ftpClient.login(username, password);
}

public List<FTPFile> getDirectory(String directoryPath) throws Exception {
    FTPFile[] files = ftpClient.listFiles(directoryPath);
    List<FTPFile> result = new ArrayList<FTPFile>();
    for (FTPFile ftpFile : files) {
        if (ftpFile.getTimestamp().getTime().getTime() >= Long.parseLong("1451606400000")) {
            result.add(ftpFile);
        }
    }
    return result;
}

public static void main(String[] args) {
    try {
        FtpServerConnector ftpServerConnector = new FtpServerConnector();
        List<FTPFile> folders = ftpServerConnector.getDirectory("/");
        for (FTPFile ftpFile : folders) {
            System.out.println(ftpFile.getName());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

There is the Exception:

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.read(BufferedReader.java:182)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.port(FTP.java:932)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:812)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:759)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3293)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3271)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)
at com.kianaanalytics.eventManagement.util.FtpServerConnector.getDirectory(FtpServerConnector.java:38)
at com.kianaanalytics.eventManagement.util.ImportWorker.getAllNewFairFolders(ImportWorker.java:19)
Jonas Wolff
  • 203
  • 1
  • 4
  • 11
  • 1
    ftpClient = new org.apache.commons.net.ftp.FTPClient(); ftpClient.setConnectTimeout(7200000); ftpClient.setDefaultTimeout(720000); ftpClient.connect(hostName); ftpClient.setKeepAlive(true); ftpClient.setControlKeepAliveReplyTimeout(3000); ftpClient.setControlKeepAliveTimeout(10); ftpClient.setBufferSize(1024*1024); – cvsr.sarma Jun 07 '17 at 19:14
  • Note that there are setters with timeout parameters in seconds (e.g. setControlKeepAliveTimeout) and other setters with timeout parameters in milliseconds (e.g. setControlKeepAliveReplyTimeout). – S. Doe Nov 17 '20 at 21:03

1 Answers1

0

It appeared that the directory has "too many" files inside of it - enough to be too long to throw SocketTimeout Exception.

You didn't mention what version are you using in FTP-client, This issue seems to handle something similar - I would consider upgrading version if its lower than 3.4.

https://issues.apache.org/jira/browse/NET-552

2Big2BeSmall
  • 1,348
  • 3
  • 20
  • 40