我的ftp上传后,俄语字符被销毁

时间:2013-02-28 06:40:56

标签: .net encoding character-encoding ftpwebrequest

上下文如下:我使用俄语文本回放xml文件,我的FileSystemWacher on Create事件将它们传输到内部ftp服务器,如下所示:

public static FtpWebResponse UploadFile(string localFile,string ftpFile,NetworkCredential credentials)         {

        //Thread.Sleep(2000);
        // Get the object used to communicate with the server.

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpFile);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UseBinary = true;
        request.Proxy = null;

        request.Credentials = credentials;


        using (FileStream sourceStream = new FileStream(localFile,FileMode.Open))
        using (Stream requestStream = request.GetRequestStream())
        {
            CopyStream(sourceStream, requestStream);
        }

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        return response;

    }

private static void CopyStream(Stream input,Stream output)         {             byte [] buffer = new byte [32768];             而(真)             {                 int read = input.Read(buffer,0,buffer.Length);                 if(读< = 0)                     返回;                 output.Write(buffer,0,read);             }         }

转移后,在ftp端,文件看起来像这样:

<OTPR consignment_number="33504655" weight_of_load="57160" forwarding_railway="22" forwarding_station="330206" consignor="3590" name_of_consignor="пїЅпїЅпїЅ "пїЅпїЅпїЅпїЅпїЅпїЅIпїЅ-пїЅпїЅпїЅпїЅIпїЅпїЅпїЅпїЅпїЅ". 18011. пїЅ. пїЅпїЅпїЅпїЅпїЅпїЅпїЅ. пїЅпїЅпїЅ.пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ, пїЅпїЅпїЅ.1 пїЅпїЅIпїЅ 225. 0472719922." destination_railway="53" destination_station="517508" consignee="9999" name_of_consignee="пїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅ." packing="2" nhm="12019000" name_of_load="пїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅ" dt="24012013" />

不像原版:

<OTPR consignment_number="33504671" weight_of_load="58740" forwarding_railway="22" forwarding_station="330206" consignor="3590" name_of_consignor="ТОВ "КОМПАНIЯ-АГРОIНВЕСТ". 18011. М. ЧЕРКАСИ. ВУЛ.ДОБРОВОЛЬСЬКОГО, БУД.1 ОФIС 225. 0472719922." destination_railway="53" destination_station="517508" consignee="9999" name_of_consignee="БУНГЕ РОМАНИЯ СРЛ." packing="2" nhm="12019000" name_of_load="БОБЫ СОЕВЫЕ" dt="24012013" /> 

1 个答案:

答案 0 :(得分:0)

这是一个老问题,但在处理一些字符编码问题时我遇到了它。这是我找到的。

我认为您需要指定字符编码。

我读到1251是俄语的编码,但我之前从未做过俄语,所以你的里程可能会有所不同。

string url = getUrl();

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Ftp.UploadFile;            
request.Credentials = new NetworkCredential(destination.UserName, destination.Password);
request.UseBinary = true;                        

byte[] bytesToSend = Encoding.GetEncoding(1251).GetBytes(stringToSendToFtp.ToCharArray());

Stream requestStream = request.GetRequestStream();
requestStream.Write(bytesToSend, 0, bytesToSend.Length);
requestStream.Close();            

FtpWebResponse response = (FtpWebResponse)request.GetResponse();