Monotouch - 通过 FTP 下载时出错(仅 zip 文件)

发布于 2024-10-15 15:18:46 字数 1540 浏览 4 评论 0原文

我使用此代码通过 FTP 从服务器下载文件。它几乎适用于所有扩展名(pdf、html、jpg...),但由于某种原因,所有 zip 文件下载时都会出现一些错误:

public static FtpStatusCode Download(string destinationFile, Uri downloadUri, string userName, string password)
    {
        try
        {

            if (downloadUri.Scheme != Uri.UriSchemeFtp)
            {
                throw new ArgumentException("Invalid FTP site");
            }
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
            ftpRequest.Credentials = new NetworkCredential(userName, password);
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            ftpRequest.UseBinary =true;
            ftpRequest.UsePassive = true;

            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream stream = null;
            StreamReader reader = null;
            StreamWriter writer = null;

            try
            {
                stream = ftpResponse.GetResponseStream();

                reader = new StreamReader(stream, Encoding.UTF8);
                writer = new StreamWriter(destinationFile, false);
                writer.Write(reader.ReadToEnd());

                return ftpResponse.StatusCode;
            }
            finally
            {

                stream.Close(); 
                reader.Close();
                writer.Close();     
            }

        }

        catch (Exception ex)
        {
            throw ex;
        }
    }

有人知道原因或可以告诉解决方案吗?

问候,
克劳迪奥

I'm using this code for download files from a server via FTP. It works fine with almost all extensions(pdf, html, jpg...) but for some reason, all zip files are downloaded with some erros:

public static FtpStatusCode Download(string destinationFile, Uri downloadUri, string userName, string password)
    {
        try
        {

            if (downloadUri.Scheme != Uri.UriSchemeFtp)
            {
                throw new ArgumentException("Invalid FTP site");
            }
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
            ftpRequest.Credentials = new NetworkCredential(userName, password);
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            ftpRequest.UseBinary =true;
            ftpRequest.UsePassive = true;

            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream stream = null;
            StreamReader reader = null;
            StreamWriter writer = null;

            try
            {
                stream = ftpResponse.GetResponseStream();

                reader = new StreamReader(stream, Encoding.UTF8);
                writer = new StreamWriter(destinationFile, false);
                writer.Write(reader.ReadToEnd());

                return ftpResponse.StatusCode;
            }
            finally
            {

                stream.Close(); 
                reader.Close();
                writer.Close();     
            }

        }

        catch (Exception ex)
        {
            throw ex;
        }
    }

Does anybody know the reason or can tell a solution?

Regards,
Claudio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2024-10-22 15:18:46

您正在使用 StreamReader 来传输信息,这会解码不是有效 UTF8 代码的二进制数据,将其转换为 UCS2 行,然后重新编码结果。

您应该在没有 StreamReader 和 StreamWriter 的情况下执行复制。

You are using a StreamReader to transfer your information, which is decoding binary data that is not valid UTF8 code, transforming it into lines of UCS2 and then re-encoding the result.

You should perform a copy without the StreamReader and the StreamWriter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文