更改FileStream写入编码类型

时间:2014-04-19 09:29:59

标签: 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(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吗? 我该如何修改该代码?

2 个答案:

答案 0 :(得分:2)

理论上,下面的内容应该有效,但它取决于响应流是否可以与流读取器一起工作。 使用不同的编码进行编写很简单,您只需使用一个编写器(基于文本编写器)而不是文件流。但是,您无法直接写入字节,因为您必须编写格式正确的文本。为此,必须使用正确的原始编码将字节转换为文本(字符缓冲区)。

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

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

答案 1 :(得分:0)

您可以将它包装在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