2

上下文如下:我接收带有俄语文本的 xml 文件,我的 FileSystemWacher on Create 事件将它们传输到内部 ftp 服务器,如下所示:

公共静态 FtpWebResponse UploadFile(字符串 localFile,字符串 ftpFile,NetworkCredential 凭据){

        //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]; while (true) { int read = input.Read(buffer, 0, buffer.Length); 如果(读取 <= 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" /> 
4

1 回答 1

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();            
于 2013-09-26T18:15:02.507 回答