问题描述
我正在尝试使用以下代码将文件创建到 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 nameI 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 字符的文件名,例如希腊语、德语等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!