之前寫過一篇System.Net.WebClient具名登入FTP的傳檔路徑問題,當時的結論認為WebClient在切換路徑時一律會加上user1,故無法存取user Home目錄以外的其他路徑。今天同事無意試出ftp:// ftpserver//booFolder//fooFolder//test.txt的寫法,能使用絕對路徑找到檔案。進一步研究才知道,ftp:// URI是可以指定絕對路徑的!

找到一篇MSDN討論區文章,發現原來只要寫成ftp:// ftpserver//booFolder/fooFolder/test.txt就可以指定絕對路徑!(同事寫法中後面兩個/可以不用重複,是多餘的) 關於這項規格,我所找到最權威的文件是IETF的Internet Draft – A FTP URL Format:

For example, the URL <URL:ftp:// myname@host.dom/%2Fetc/motd> is 
interpreted by FTP-ing to "host.dom", logging in as "myname"
(prompting for a password if it is asked for), and then executing
"CWD /etc" and then "RETR motd". This has a different meaning from
<URL:ftp:// myname@host.dom/etc/motd> which would "CWD etc" and then
"RETR motd"; the initial "CWD" might be executed relative to the
default directory for "myname". On the other hand,
<URL:ftp:// myname@host.dom//etc/motd>, would "CWD " with a null
argument, then "CWD etc", and then "RETR motd".
Implementors should note that doing a "RETR 
<cwd1>/.../<cwdN>/<name>" may work on some systems, but clients
MUST NOT rely upon it.  In some situations, this may be the only
way to access a resource.  This is the case if one or more of the
directories <cwd1>...<cwdN-1> is access controlled, but the final
one is not.

文件已經有點歷史了(1997),而負責解析FTP URI的元件、程式也不盡然會100%依照它實做,例如System.Net.WebClient就接受似ftp:// host.dom//etc/motd的寫法,不限定非寫成ftp:// host.com/%2Fetc/motd不可。

經實測驗證,在遇到System.Net.WebClient具名登入FTP需指定特殊路徑時,可使用以下兩種寫法擇一:

ftp:// ftpserver//booFolder/fooFolder/test.txt 或
ftp:// ftpserver/%2FbooFolder/fooFolder/test.txt


Comments

Be the first to post a comment

Post a comment


22 + 16 =