Ftp使用utf-8字符创建文件名,如希腊语,德语等 [英] Ftp create a filename with utf-8 chars such as greek, german etc

查看:308
本文介绍了Ftp使用utf-8字符创建文件名,如希腊语,德语等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$($) c $ c> string username =name; 
string password =password;
string remotefolder =ftp://ftp.myhost.gr/public_html/test/;
string remoteFileName =δοκιμαστικόαρχείοüß-äCopy.txt;
string localFile = @C:\test\δοκιμαστικόαρχείο - 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



1)我该怎么办? p>

一旦我更改了我的区域设置,有一些改进 - >非Unicode程序的当前语言为希腊语,但我仍然想念德语的chars。



2)为什么ac#程序依赖于此设置?为了避免这种设置的依赖,我应该遵循一种特殊的方法?



编码恶梦再次出现:(

解决方案

仅仅编码是不够的您的字符串为UTF8,并将其作为文件名发送到FTP服务器。以前所有的FTP服务器都理解为只有ASCII,现在才能保持向后兼容性 - 即使它们是Unicode感知的 - 当它们启动时将所有的文件作为ASCII格式复制。



为了使其全部工作(您的程序)必须首先检查您的服务器的功能。服务器在客户端连接后发送其功能 - 在您的情况下,您必须检查 FEAT UTF8 。如果您的服务器发送 - 这意味着它了解UTF8。尽管如此 - 即使它明白了 - 你必须明确地告诉它,从现在开始,你将发送你的文件名UTF8编码,现在它是你的程序缺少的东西(你的服务器支持utf8,如你所说)。 >

您的客户端必须向FTP服务器发送以下 OPTS UTF8 ON 。发送后,您可以使用UTF8或UTF8-ish(可以这么说)给您的服务器。



请阅读此处了解详情文件传输协议的国际化


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:\test\δοκιμαστικό αρχείο - 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);

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) What can I do with that?

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) Why does a c# program depend on this setting? Is there a special methodology i should follow in order to avoid dependency from this setting?

Encoding nightmares arose again :(

解决方案

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.

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).

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.

Read here for details Internationalization of the File Transfer Protocol

这篇关于Ftp使用utf-8字符创建文件名,如希腊语,德语等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆