問題描述
我正在嘗試使用以下代碼將文件創建到 ftp 服務器(我也嘗試使用 UseBinary 選項 true 和 false)
I am trying to create a file to an ftp server with the following code (where I also tried with UseBinary option true and false)
string username = "name";
string password = "password";
string remotefolder = "ftp://ftp.myhost.gr/public_html/test/";
string remoteFileName = "δοκιμαστικ? αρχε?οü?-?Copy.txt";
string localFile = @"C: estδοκιμαστικ? αρχε?ο - Copy.txt";
String ftpname = "ftp://ftp.myhost.gr/public_html/test" + @"/" + Uri.EscapeUriString(Program.remoteFileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpname);
request.Proxy = null;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
//request.UseBinary = false;
byte[] content = System.IO.File.ReadAllBytes(localFile);
byte[] fileContents = new Byte[content.Length];
Array.Copy(content, 0, fileContents, 0, content.Length);
using (Stream uploadStream = request.GetRequestStream())
{
int contentLength = fileContents.Length;
uploadStream.Write(fileContents, 0, contentLength);
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine(response.ExitMessage);
問題是我的 ftp 服務器上的文件沒有獲得名稱我要求其中包含英語、希臘語和德語字符 --> "δοκιμαστικ? αρχε?οü?-?Copy.txt
The problem is that file at my ftp server does not get the name I request which contains English, greek and german characters --> "δοκιμαστικ? αρχε?οü?-?Copy.txt
1) 我能用它做什么?
更改區域設置后會有一些改進 --> 非 Unicode 程序的當前語言為希臘語,但我仍然想念德語字符.
There is some improvement once I change my regional settings--> Current language for non-Unicode programs to Greek Language but I still miss the german chars.
2) 為什么 c# 程序依賴于這個設置?為了避免依賴此設置,我應該遵循一種特殊的方法嗎?
編碼噩夢再次出現:(
推薦答案
僅將字符串編碼為 UTF8 并將其作為文件名發送到 FTP 服務器是不夠的.過去所有 FTP 服務器都只理解 ASCII,而現在為了保持向后兼容性——即使它們支持 Unicode——當它們啟動時,它們也將所有文件名都視為 ASCII.
It is not enough for you just to encode your string as UTF8 and send it as filename to FTP server. In the past all FTP servers understood ASCII only and nowadays to maintain backward compatibility - even if they are Unicode aware - when they start they treat all filenemes as ASCII too.
要使這一切正常運行,您(您的程序)必須首先檢查您的服務器的功能.服務器在客戶端連接后發送其功能 - 在您的情況下,您必須檢查 FEAT UTF8.如果您的服務器發送它 - 這意味著它理解 UTF8.盡管如此 - 即使它理解它 - 你必須明確告訴它,從現在開始你將發送你的文件名 UTF8 編碼,現在它是你的程序缺少的東西(因為你的服務器支持 utf8,正如你所說的).
To make it all work you (your program) must first check what your server is capable of. Servers send their features after client connects - in your case you must check for FEAT UTF8. If your server sends that - it means it understands UTF8. Nevertheless - even if it understands it - you must tell it explicitly that from now on you will send your filenames UTF8 encoded and now it is the stuff that your program lacks (as your server supports utf8 as you've stated).
您的客戶端必須向 FTP 服務器發送以下 OPTS UTF8 ON.發送后,您可以使用 UTF8 或對您的服務器說 UTF8-ish(可以這么說).
Your client must send to FTP server the following OPTS UTF8 ON. After sending that you may use UTF8 or speak UTF8-ish (so to speak) to your sever.
閱讀此處了解詳情文件傳輸協議的國際化
這篇關于Ftp 創建帶有 utf-8 字符的文件名,例如希臘語、德語等的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!