【问题标题】:Changing FileStream Write encoding type更改 FileStream 写入编码类型
【发布时间】:2014-06-03 19:14:25
【问题描述】:

这是我的代码:

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 吗? 如何修改该代码?

【问题讨论】:

    标签: c# asp.net file encoding character-encoding


    【解决方案1】:

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

        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();
            }
        }
    

    如果流式读取器出现问题,您也可以先下载字节,然后在已经下载的字节上使用流式读取器。

    【讨论】:

      【解决方案2】:

      你可以把它包装在 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();
          }
      

      希望对你有帮助

      你看看这里:https://stackoverflow.com/ answer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 2013-11-02
        相关资源
        最近更新 更多