我正在使用此代码通过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;
}
}
有人知道原因还是能说出解决方案?
的问候,
克劳迪奥
答案 0 :(得分:3)
您正在使用StreamReader传输您的信息,该信息正在解码无效UTF8代码的二进制数据,将其转换为UCS2行,然后重新编码结果。
您应该在没有StreamReader和StreamWriter的情况下执行复制。