Monotouch - 通过 FTP 下载时出错(仅 zip 文件)
我使用此代码通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用 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.