FTP中MLST概要解讀---解決獲取ftpFile為null的另外一種方式


零、引言

之前寫FTP工具庫,用的是ftp4j,他使用其他非常簡單方便,但是在細節上提供的可選項比較少(當然也可能是我了解不夠深刻)

最新的項目重寫了FTP工具類,選擇了apache net中的ftp庫,選擇apache的原因有如下幾個:1是我相信apche 2是它的注釋完善(apache的代碼注釋值得每一位程序猿學習) 3是提供的可選配置(FTPConfig)有跟多選擇(比如主動被動模式,斷點續傳等)。

本人在使用ftp4j判定文件是否存在的時候,通過API(具體那個忘了)獲取FTPFile對象時,在部分FTP服務時(filezilla)會遇到返回值為null的問題(備注:原因是時間格式化的問題),當時解決判定文件是否存在改用只通過獲取文件名來解決。

本次改用apache net的時候,在使用API listFiles() 獲取的也是null,經過詳細查看源碼,發現了一個API是 mlistFile() 這樣獲取結果OK。

一、Apache的mlistFile源碼分析

源碼如下:

    /**
     * Get file details using the MLST command
     *
     * @param pathname the file or directory to list, may be {@code null}
     * @return the file details, may be {@code null}
     * @throws IOException on error
     * @since 3.0
     */
    public FTPFile mlistFile(String pathname) throws IOException
    {
        boolean success = FTPReply.isPositiveCompletion(sendCommand(FTPCmd.MLST, pathname));
        if (success){
            String reply = getReplyStrings()[1]; //5:check /* check the response makes sense.
             * Must have space before fact(s) and between fact(s) and filename
             * Fact(s) can be absent, so at least 3 chars are needed.
             */
            if (reply.length() < 3 || reply.charAt(0) != ' ') {
                throw new MalformedServerReplyException("Invalid server reply (MLST): '" + reply + "'");
            }
            String entry = reply.substring(1); // skip leading space for parser
            return MLSxEntryParser.parseEntry(entry);
        } else {
            return null;
        }
    }

我看源碼大概分析出了2點:

1. 此方法調用的是MLST,而listFile調用的是LIST。問題來了:什么是MLST??

2. 代碼方法體第4行: String reply = getReplyStrings()[1]; 獲取返回碼其目的是為了判定FTP服務器是否支持MLST??有些FTPServer不支持?

二、什么是MLST(紅色部分)

Older servers supports LIST command only for directory listing, this way the FTP client gets a non-userfriendly raw format for parsing, and only FTP clients knows what it meaning. Since the file timestamp based on the server timezone, it makes more different to doing FTP synchronize with folders and files because of there is no way to get current file timestamp in the server.
舊的服務對目錄列表僅支持LIST命令,這樣FTP客戶端會獲得一個非用戶友好的待解析的原始格式,僅僅只有FTP客戶端知道它的意義。由於文件的時間戳基於服務器的時區,它使得在FTP對目錄和文件同步時有許多的不同,因為沒有方法獲得服務器上當前文件的時間戳。
For example there is a normally LIST format of raw directory:
-rw-r--r-- 1 user user 7080 Mar 9 05:24 faq.html
例如:這是對原始目錄的普通LIST命令格式
-rw-r--r-- 1 user user 7080 Mar 9 05:24 faq.html
Is it readable for you?
是否可讀呢?
The MLSD command provided by newer servers to gives users a standarded, detailed, readable directory listing, by sending MLSD command through FTP clients,the server returns accurate file information such as file create time, modified time, size and file owner. Since MLSD directory listing includes file modified time in UTC, so it's very useful for FTP client to converts remote file's timestamp to your Local time when synchronize folders. Also the MLST command could be used to get timestamp of single remote file only.
新的服務器提供的MLSD命令通過FTP客戶端發送MLSD命令,服務器收集文件信息,如文件創建時間,修改時間,文件大小及文件所有用,向用戶返回一個標准,詳細且可讀格式的目錄列表。由於MLSD目錄列表包含UTC格式的文件修改時間,因此這對於FTP客戶端非常有用,當需要同步目錄時,它可轉換遠程文件的時間戳到你的本地時間。同時,MLST命令也被用於獲得單個遠程文件的時間戳。

三、結尾

最近打王者有些沉迷,該反省。

學知識,做技術,不能不求甚解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM