C# 更改FileStream写入编码类型

C# 更改FileStream写入编码类型,c#,asp.net,file,encoding,character-encoding,C#,Asp.net,File,Encoding,Character Encoding,这是我的代码: public static string DownloadFile(string FtpUrl, string FileNameToDownload, string userName, string password, string tempDirPath) { string ResponseDescription = ""; string PureFileName = new FileInfo(Fil

这是我的代码:

public static string DownloadFile(string FtpUrl, string FileNameToDownload,
                   string userName, string password, string tempDirPath)
    {
        string ResponseDescription = "";
        string PureFileName = new FileInfo(FileNameToDownload).Name;
        string DownloadedFilePath = tempDirPath + "/" + PureFileName;
        string downloadUrl = String.Format("{0}/{1}", FtpUrl, FileNameToDownload);
        FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
        req.Method = WebRequestMethods.Ftp.DownloadFile;
        req.Credentials = new NetworkCredential(userName, password);
        req.UseBinary = true;
        req.Proxy = null;
        try
        {
            FtpWebResponse response = (FtpWebResponse)req.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] buffer = new byte[2048];
            FileStream fs = new FileStream(DownloadedFilePath, FileMode.Create);
            int ReadCount = stream.Read(buffer, 0, buffer.Length);
            while (ReadCount > 0)
            {

              fs.Write(buffer, 0, ReadCount);
              ReadCount = stream.Read(buffer, 0, buffer.Length);
            }
            ResponseDescription = response.StatusDescription;
            fs.Close();

            stream.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return ResponseDescription;
    }

}
这段代码从ftp服务器下载一个文件,并将其写入服务器中的特定路径。 但保存文件的编码不是UTF-8。 我想将文件的编码类型更改为UTF-8。 我必须使用StreamReader吗?
如何修改该代码?

您可以将其包装在StreaWriter中:

try
    {
        FtpWebResponse response = (FtpWebResponse)req.GetResponse();
        Stream stream = response.GetResponseStream();
        byte[] buffer = new byte[2048];
        var sw = new StreamWriter( new FileStream(DownloadedFilePath, FileMode.Create),
    Encoding.UTF8);
        int ReadCount = stream.Read(buffer, 0, buffer.Length);
        while (ReadCount > 0)
        {

           sw.Write(buffer, 0, ReadCount);
          ReadCount = stream.Read(buffer, 0, buffer.Length);
        }
        ResponseDescription = response.StatusDescription;
        sw.Close();

        stream.Close();
    }
我希望这会有帮助


您可以在这里查看:

理论上,下面的内容应该可以工作,但这取决于responsestream是否可以与streamreader一起工作。 使用不同的编码编写很容易,您可以简单地使用streamwriter(基于textwriter)而不是filestream。但是,您不能直接写入字节,因为您必须写入格式正确的文本。为此,必须使用正确的原始编码将字节转换为文本(字符缓冲区)

    char[] buffer = new char[2048]; //or 1024 if you want to keep the same block size
    using (var reader = new StreamReader(stream, Encoding.Unicode)) // <= Or whatever encoding the orignal is
    {
        using (var tw = new StreamWriter(DownloadedFilePath, false, Encoding.UTF8)) //streamwriter instead of filestream
        {                
            while (true)
            {
                int ReadCount = reader.Read(buffer, 0, buffer.Length);
                if (ReadCount == 0) break;
                tw.Write(buffer, 0, ReadCount);                    
            }
            ResponseDescription = response.StatusDescription;
            stream.Close();
            tw.Close();
        }
    }
char[]buffer=新字符[2048]//或1024,如果要保持相同的块大小
使用(var reader=newstreamreader(stream,Encoding.Unicode))//