Apache Java FTPclient按时间戳排序

时间:2018-03-28 15:51:17

标签: java apache ftp ftp-client

是否可以通过时间戳使用Apache ftp客户端和列出目录内容? 我在做:

mFtpClient.listNames("-t .");

但是返回一个空列表。

编辑:我正在使用Filezilla Server。

1 个答案:

答案 0 :(得分:0)

我已经用这种方式解决了:

public class CustomFTPClient extends FTPClient{

    public String[] listNamesOrdered(String pathname) throws IOException
    {
        if(pathname == null) pathname = "";
        StringBuilder sb = new StringBuilder(pathname.length() + 3);
        sb.append("-t -A1 ");
        sb.append(pathname);
        pathname = sb.toString();

        Socket socket = _openDataConnection_(FTPCmd.LIST, getListArguments(pathname));

        if (socket == null) {
            return null;
        }

        BufferedReader reader =
                new BufferedReader(new InputStreamReader(socket.getInputStream(), getControlEncoding()));

        ArrayList<String> results = new ArrayList<String>();
        String line;
        while ((line = reader.readLine()) != null) {
            results.add(line);
        }

        reader.close();
        socket.close();

        if (completePendingCommand())
        {
            String[] names = new String[ results.size() ];
            return results.toArray(names);
        }

        return null;
    }

}

我复制了listNames方法,并使用带-t参数的LIST命令。