C# FTP 下载RAR文件打不开
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
ftpRequest.Credentials = new NetworkCredential(userName, password);// 传入用户名和密码
ftpRequest.KeepAlive = false;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;// 设置下载方式
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
{
stream = ftpResponse.GetResponseStream();
reader = new StreamReader(stream, Encoding.Default);
writer = new StreamWriter(destinationFile, false, Encoding.Default);//true, Encoding.Default
writer.Write(reader.ReadToEnd());
return ftpResponse.StatusCode;
}
问题描述:
可以下载,但是下载后的winrar解压错误,而且比FTP服务器上的稍微小一些
就这么多分了,全给你们了,谢谢指点!
------解决方案--------------------用这个里面的封装好的下载方法试试看。http://blog.csdn.net/chinacsharper/article/details/9501773
------解决方案--------------------RAR 是二进制文件,不能用 StreamReader 和 StreamWriter 来操作,要直接用 FileStream 保存。
FileStream fs = new FileStream(destinationFile, FileMode.Create);
int n = 0;
byte[] buff = new byte[4096];
while ((n = stream.Read(buff, 0, buff.Length)) > 0)
{
fs.Write(buff, 0, n);
}
fs.Close();