2 shaoan886 shaoan886 于 2014.11.24 11:19 提问

C# 通过ftp 下载文件失效

C# 通过ftp 下载文件失效?
下载下来的文件大小与服务器上大小相同,但是无法使用此文件.
也不报错,没异常,偶发性的.
通过vpn连到公司内网,再下载文件出现这样的情况比较高.
下载的文件是图片.查看图片时 下载的文件可能已损坏损毁或者过大.
蛋碎...跪求大神解决

1个回答

shaoan886
shaoan886   2014.11.24 11:22

现在我来贴一下代码` FileStream outputStream = FileOperate.FileCreateGetStream(filePath + "\" + fileName);
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Proxy = null;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
// long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;

byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);

           //ftpStream.pe
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);

            }
            outputStream.Flush();
            ftpStream.Close();
            response.Close();
            //outputStream.Close();
            //response.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally 
        {
            outputStream.Close();
            outputStream.Dispose();
        }`
Csdn user default icon
上传中...
上传图片
插入图片